├── .gitattributes ├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ └── contents.xcworkspacedata ├── CropImageDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ ├── IDEWorkspaceChecks.plist │ └── swiftpm │ └── Package.resolved ├── CropImageDemo ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ └── LaunchScreen.storyboard ├── ContentView.swift ├── CropImageDemo.entitlements ├── Info.plist ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json ├── SceneDelegate.swift └── demo.png ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── CropImageView │ └── CropImageView.swift └── Tests ├── CropImageViewTests ├── CropImageViewTests.swift └── XCTestManifests.swift └── LinuxMain.swift /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | ## Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CropImageDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 52; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 839FEE2F24DD470F00236B88 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839FEE2E24DD470F00236B88 /* AppDelegate.swift */; }; 11 | 839FEE3124DD470F00236B88 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839FEE3024DD470F00236B88 /* SceneDelegate.swift */; }; 12 | 839FEE3324DD470F00236B88 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 839FEE3224DD470F00236B88 /* ContentView.swift */; }; 13 | 839FEE3524DD471700236B88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 839FEE3424DD471700236B88 /* Assets.xcassets */; }; 14 | 839FEE3824DD471700236B88 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 839FEE3724DD471700236B88 /* Preview Assets.xcassets */; }; 15 | 839FEE3B24DD471700236B88 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 839FEE3924DD471700236B88 /* LaunchScreen.storyboard */; }; 16 | 839FEE4624DD4C6700236B88 /* demo.png in Resources */ = {isa = PBXBuildFile; fileRef = 839FEE4524DD4C6700236B88 /* demo.png */; }; 17 | 839FEE5624DE307200236B88 /* CropImageView in Frameworks */ = {isa = PBXBuildFile; productRef = 839FEE5524DE307200236B88 /* CropImageView */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 839FEE2B24DD470F00236B88 /* CropImageDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CropImageDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 839FEE2E24DD470F00236B88 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | 839FEE3024DD470F00236B88 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 24 | 839FEE3224DD470F00236B88 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 25 | 839FEE3424DD471700236B88 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 839FEE3724DD471700236B88 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 27 | 839FEE3A24DD471700236B88 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 28 | 839FEE3C24DD471700236B88 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29 | 839FEE4224DD473500236B88 /* CropImageDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CropImageDemo.entitlements; sourceTree = ""; }; 30 | 839FEE4524DD4C6700236B88 /* demo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = demo.png; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 839FEE2824DD470F00236B88 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | 839FEE5624DE307200236B88 /* CropImageView in Frameworks */, 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 839FEE2224DD470F00236B88 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 839FEE2D24DD470F00236B88 /* CropImageDemo */, 49 | 839FEE2C24DD470F00236B88 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 839FEE2C24DD470F00236B88 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 839FEE2B24DD470F00236B88 /* CropImageDemo.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 839FEE2D24DD470F00236B88 /* CropImageDemo */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 839FEE4224DD473500236B88 /* CropImageDemo.entitlements */, 65 | 839FEE2E24DD470F00236B88 /* AppDelegate.swift */, 66 | 839FEE3024DD470F00236B88 /* SceneDelegate.swift */, 67 | 839FEE3224DD470F00236B88 /* ContentView.swift */, 68 | 839FEE4524DD4C6700236B88 /* demo.png */, 69 | 839FEE3424DD471700236B88 /* Assets.xcassets */, 70 | 839FEE3924DD471700236B88 /* LaunchScreen.storyboard */, 71 | 839FEE3C24DD471700236B88 /* Info.plist */, 72 | 839FEE3624DD471700236B88 /* Preview Content */, 73 | ); 74 | path = CropImageDemo; 75 | sourceTree = ""; 76 | }; 77 | 839FEE3624DD471700236B88 /* Preview Content */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 839FEE3724DD471700236B88 /* Preview Assets.xcassets */, 81 | ); 82 | path = "Preview Content"; 83 | sourceTree = ""; 84 | }; 85 | /* End PBXGroup section */ 86 | 87 | /* Begin PBXNativeTarget section */ 88 | 839FEE2A24DD470F00236B88 /* CropImageDemo */ = { 89 | isa = PBXNativeTarget; 90 | buildConfigurationList = 839FEE3F24DD471700236B88 /* Build configuration list for PBXNativeTarget "CropImageDemo" */; 91 | buildPhases = ( 92 | 839FEE2724DD470F00236B88 /* Sources */, 93 | 839FEE2824DD470F00236B88 /* Frameworks */, 94 | 839FEE2924DD470F00236B88 /* Resources */, 95 | ); 96 | buildRules = ( 97 | ); 98 | dependencies = ( 99 | ); 100 | name = CropImageDemo; 101 | packageProductDependencies = ( 102 | 839FEE5524DE307200236B88 /* CropImageView */, 103 | ); 104 | productName = CropImageDemo; 105 | productReference = 839FEE2B24DD470F00236B88 /* CropImageDemo.app */; 106 | productType = "com.apple.product-type.application"; 107 | }; 108 | /* End PBXNativeTarget section */ 109 | 110 | /* Begin PBXProject section */ 111 | 839FEE2324DD470F00236B88 /* Project object */ = { 112 | isa = PBXProject; 113 | attributes = { 114 | LastSwiftUpdateCheck = 1160; 115 | LastUpgradeCheck = 1160; 116 | ORGANIZATIONNAME = Xingfa; 117 | TargetAttributes = { 118 | 839FEE2A24DD470F00236B88 = { 119 | CreatedOnToolsVersion = 11.6; 120 | }; 121 | }; 122 | }; 123 | buildConfigurationList = 839FEE2624DD470F00236B88 /* Build configuration list for PBXProject "CropImageDemo" */; 124 | compatibilityVersion = "Xcode 9.3"; 125 | developmentRegion = en; 126 | hasScannedForEncodings = 0; 127 | knownRegions = ( 128 | en, 129 | Base, 130 | ); 131 | mainGroup = 839FEE2224DD470F00236B88; 132 | packageReferences = ( 133 | 839FEE5424DE307200236B88 /* XCRemoteSwiftPackageReference "CropImageView" */, 134 | ); 135 | productRefGroup = 839FEE2C24DD470F00236B88 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 839FEE2A24DD470F00236B88 /* CropImageDemo */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | 839FEE2924DD470F00236B88 /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 839FEE4624DD4C6700236B88 /* demo.png in Resources */, 150 | 839FEE3B24DD471700236B88 /* LaunchScreen.storyboard in Resources */, 151 | 839FEE3824DD471700236B88 /* Preview Assets.xcassets in Resources */, 152 | 839FEE3524DD471700236B88 /* Assets.xcassets in Resources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXResourcesBuildPhase section */ 157 | 158 | /* Begin PBXSourcesBuildPhase section */ 159 | 839FEE2724DD470F00236B88 /* Sources */ = { 160 | isa = PBXSourcesBuildPhase; 161 | buildActionMask = 2147483647; 162 | files = ( 163 | 839FEE2F24DD470F00236B88 /* AppDelegate.swift in Sources */, 164 | 839FEE3124DD470F00236B88 /* SceneDelegate.swift in Sources */, 165 | 839FEE3324DD470F00236B88 /* ContentView.swift in Sources */, 166 | ); 167 | runOnlyForDeploymentPostprocessing = 0; 168 | }; 169 | /* End PBXSourcesBuildPhase section */ 170 | 171 | /* Begin PBXVariantGroup section */ 172 | 839FEE3924DD471700236B88 /* LaunchScreen.storyboard */ = { 173 | isa = PBXVariantGroup; 174 | children = ( 175 | 839FEE3A24DD471700236B88 /* Base */, 176 | ); 177 | name = LaunchScreen.storyboard; 178 | sourceTree = ""; 179 | }; 180 | /* End PBXVariantGroup section */ 181 | 182 | /* Begin XCBuildConfiguration section */ 183 | 839FEE3D24DD471700236B88 /* Debug */ = { 184 | isa = XCBuildConfiguration; 185 | buildSettings = { 186 | ALWAYS_SEARCH_USER_PATHS = NO; 187 | CLANG_ANALYZER_NONNULL = YES; 188 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 189 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 190 | CLANG_CXX_LIBRARY = "libc++"; 191 | CLANG_ENABLE_MODULES = YES; 192 | CLANG_ENABLE_OBJC_ARC = YES; 193 | CLANG_ENABLE_OBJC_WEAK = YES; 194 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 195 | CLANG_WARN_BOOL_CONVERSION = YES; 196 | CLANG_WARN_COMMA = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 199 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 200 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 201 | CLANG_WARN_EMPTY_BODY = YES; 202 | CLANG_WARN_ENUM_CONVERSION = YES; 203 | CLANG_WARN_INFINITE_RECURSION = YES; 204 | CLANG_WARN_INT_CONVERSION = YES; 205 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 206 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 207 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 210 | CLANG_WARN_STRICT_PROTOTYPES = YES; 211 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 212 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 213 | CLANG_WARN_UNREACHABLE_CODE = YES; 214 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 215 | COPY_PHASE_STRIP = NO; 216 | DEBUG_INFORMATION_FORMAT = dwarf; 217 | ENABLE_STRICT_OBJC_MSGSEND = YES; 218 | ENABLE_TESTABILITY = YES; 219 | GCC_C_LANGUAGE_STANDARD = gnu11; 220 | GCC_DYNAMIC_NO_PIC = NO; 221 | GCC_NO_COMMON_BLOCKS = YES; 222 | GCC_OPTIMIZATION_LEVEL = 0; 223 | GCC_PREPROCESSOR_DEFINITIONS = ( 224 | "DEBUG=1", 225 | "$(inherited)", 226 | ); 227 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 228 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 229 | GCC_WARN_UNDECLARED_SELECTOR = YES; 230 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 231 | GCC_WARN_UNUSED_FUNCTION = YES; 232 | GCC_WARN_UNUSED_VARIABLE = YES; 233 | IPHONEOS_DEPLOYMENT_TARGET = 13.6; 234 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 235 | MTL_FAST_MATH = YES; 236 | ONLY_ACTIVE_ARCH = YES; 237 | SDKROOT = iphoneos; 238 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 239 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 240 | }; 241 | name = Debug; 242 | }; 243 | 839FEE3E24DD471700236B88 /* Release */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_ANALYZER_NONNULL = YES; 248 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 249 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 250 | CLANG_CXX_LIBRARY = "libc++"; 251 | CLANG_ENABLE_MODULES = YES; 252 | CLANG_ENABLE_OBJC_ARC = YES; 253 | CLANG_ENABLE_OBJC_WEAK = YES; 254 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 255 | CLANG_WARN_BOOL_CONVERSION = YES; 256 | CLANG_WARN_COMMA = YES; 257 | CLANG_WARN_CONSTANT_CONVERSION = YES; 258 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 259 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 260 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 261 | CLANG_WARN_EMPTY_BODY = YES; 262 | CLANG_WARN_ENUM_CONVERSION = YES; 263 | CLANG_WARN_INFINITE_RECURSION = YES; 264 | CLANG_WARN_INT_CONVERSION = YES; 265 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 266 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 267 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 268 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 269 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 270 | CLANG_WARN_STRICT_PROTOTYPES = YES; 271 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 272 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 273 | CLANG_WARN_UNREACHABLE_CODE = YES; 274 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 275 | COPY_PHASE_STRIP = NO; 276 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 277 | ENABLE_NS_ASSERTIONS = NO; 278 | ENABLE_STRICT_OBJC_MSGSEND = YES; 279 | GCC_C_LANGUAGE_STANDARD = gnu11; 280 | GCC_NO_COMMON_BLOCKS = YES; 281 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 282 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 283 | GCC_WARN_UNDECLARED_SELECTOR = YES; 284 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 285 | GCC_WARN_UNUSED_FUNCTION = YES; 286 | GCC_WARN_UNUSED_VARIABLE = YES; 287 | IPHONEOS_DEPLOYMENT_TARGET = 13.6; 288 | MTL_ENABLE_DEBUG_INFO = NO; 289 | MTL_FAST_MATH = YES; 290 | SDKROOT = iphoneos; 291 | SWIFT_COMPILATION_MODE = wholemodule; 292 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 293 | VALIDATE_PRODUCT = YES; 294 | }; 295 | name = Release; 296 | }; 297 | 839FEE4024DD471700236B88 /* Debug */ = { 298 | isa = XCBuildConfiguration; 299 | buildSettings = { 300 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 301 | CODE_SIGN_ENTITLEMENTS = CropImageDemo/CropImageDemo.entitlements; 302 | "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; 303 | CODE_SIGN_STYLE = Automatic; 304 | DEVELOPMENT_ASSET_PATHS = "\"CropImageDemo/Preview Content\""; 305 | ENABLE_PREVIEWS = YES; 306 | INFOPLIST_FILE = CropImageDemo/Info.plist; 307 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 308 | LD_RUNPATH_SEARCH_PATHS = ( 309 | "$(inherited)", 310 | "@executable_path/Frameworks", 311 | ); 312 | PRODUCT_BUNDLE_IDENTIFIER = com.xingfa.CropImageDemo; 313 | PRODUCT_NAME = "$(TARGET_NAME)"; 314 | SUPPORTS_MACCATALYST = YES; 315 | SWIFT_VERSION = 5.0; 316 | TARGETED_DEVICE_FAMILY = "1,2"; 317 | }; 318 | name = Debug; 319 | }; 320 | 839FEE4124DD471700236B88 /* Release */ = { 321 | isa = XCBuildConfiguration; 322 | buildSettings = { 323 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 324 | CODE_SIGN_ENTITLEMENTS = CropImageDemo/CropImageDemo.entitlements; 325 | "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; 326 | CODE_SIGN_STYLE = Automatic; 327 | DEVELOPMENT_ASSET_PATHS = "\"CropImageDemo/Preview Content\""; 328 | ENABLE_PREVIEWS = YES; 329 | INFOPLIST_FILE = CropImageDemo/Info.plist; 330 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 331 | LD_RUNPATH_SEARCH_PATHS = ( 332 | "$(inherited)", 333 | "@executable_path/Frameworks", 334 | ); 335 | PRODUCT_BUNDLE_IDENTIFIER = com.xingfa.CropImageDemo; 336 | PRODUCT_NAME = "$(TARGET_NAME)"; 337 | SUPPORTS_MACCATALYST = YES; 338 | SWIFT_VERSION = 5.0; 339 | TARGETED_DEVICE_FAMILY = "1,2"; 340 | }; 341 | name = Release; 342 | }; 343 | /* End XCBuildConfiguration section */ 344 | 345 | /* Begin XCConfigurationList section */ 346 | 839FEE2624DD470F00236B88 /* Build configuration list for PBXProject "CropImageDemo" */ = { 347 | isa = XCConfigurationList; 348 | buildConfigurations = ( 349 | 839FEE3D24DD471700236B88 /* Debug */, 350 | 839FEE3E24DD471700236B88 /* Release */, 351 | ); 352 | defaultConfigurationIsVisible = 0; 353 | defaultConfigurationName = Release; 354 | }; 355 | 839FEE3F24DD471700236B88 /* Build configuration list for PBXNativeTarget "CropImageDemo" */ = { 356 | isa = XCConfigurationList; 357 | buildConfigurations = ( 358 | 839FEE4024DD471700236B88 /* Debug */, 359 | 839FEE4124DD471700236B88 /* Release */, 360 | ); 361 | defaultConfigurationIsVisible = 0; 362 | defaultConfigurationName = Release; 363 | }; 364 | /* End XCConfigurationList section */ 365 | 366 | /* Begin XCRemoteSwiftPackageReference section */ 367 | 839FEE5424DE307200236B88 /* XCRemoteSwiftPackageReference "CropImageView" */ = { 368 | isa = XCRemoteSwiftPackageReference; 369 | repositoryURL = "https://github.com/zhxf2012/CropImageView"; 370 | requirement = { 371 | branch = master; 372 | kind = branch; 373 | }; 374 | }; 375 | /* End XCRemoteSwiftPackageReference section */ 376 | 377 | /* Begin XCSwiftPackageProductDependency section */ 378 | 839FEE5524DE307200236B88 /* CropImageView */ = { 379 | isa = XCSwiftPackageProductDependency; 380 | package = 839FEE5424DE307200236B88 /* XCRemoteSwiftPackageReference "CropImageView" */; 381 | productName = CropImageView; 382 | }; 383 | /* End XCSwiftPackageProductDependency section */ 384 | }; 385 | rootObject = 839FEE2324DD470F00236B88 /* Project object */; 386 | } 387 | -------------------------------------------------------------------------------- /CropImageDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CropImageDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CropImageDemo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "object": { 3 | "pins": [ 4 | { 5 | "package": "CropImageView", 6 | "repositoryURL": "https://github.com/zhxf2012/CropImageView", 7 | "state": { 8 | "branch": "master", 9 | "revision": "aba1f72d74aa2c28e06494284a78291b0bbebf94", 10 | "version": null 11 | } 12 | } 13 | ] 14 | }, 15 | "version": 1 16 | } 17 | -------------------------------------------------------------------------------- /CropImageDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CropImageDemo 4 | // 5 | // Created by Xingfa Zhou on 2020/8/7. 6 | // Copyright © 2020 Xingfa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /CropImageDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /CropImageDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /CropImageDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /CropImageDemo/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // CropImageDemo 4 | // 5 | // Created by Xingfa Zhou on 2020/8/7. 6 | // Copyright © 2020 Xingfa. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | import CropImageView 11 | 12 | struct ContentView: View { 13 | @State var showCropView = false 14 | @State private var cropedImage: UIImage? 15 | 16 | var inputImage: UIImage { 17 | return UIImage(named: "demo") ?? UIImage(systemName: "sun.haze.fill")! 18 | } 19 | 20 | var body: some View { 21 | VStack { 22 | Button(action: { 23 | self.showCropView = true 24 | }) { 25 | Text("Show the crop view") 26 | } 27 | 28 | if self.cropedImage != nil { 29 | Image(uiImage: self.cropedImage!) 30 | .resizable() 31 | .scaledToFit() 32 | .padding() 33 | } 34 | } 35 | .sheet(isPresented: $showCropView,onDismiss:finishedCrop ) { 36 | CropImageView(inputImage: self.inputImage, resultImage: self.$cropedImage, cropSize: CGSize(width: 250, height: 250)) 37 | } 38 | } 39 | 40 | func finishedCrop() { 41 | 42 | } 43 | } 44 | 45 | struct ContentView_Previews: PreviewProvider { 46 | static var previews: some View { 47 | ContentView() 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /CropImageDemo/CropImageDemo.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.network.client 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CropImageDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /CropImageDemo/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /CropImageDemo/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // CropImageDemo 4 | // 5 | // Created by Xingfa Zhou on 2020/8/7. 6 | // Copyright © 2020 Xingfa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SwiftUI 11 | 12 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | 22 | // Create the SwiftUI view that provides the window contents. 23 | let contentView = ContentView() 24 | 25 | // Use a UIHostingController as window root view controller. 26 | if let windowScene = scene as? UIWindowScene { 27 | let window = UIWindow(windowScene: windowScene) 28 | window.rootViewController = UIHostingController(rootView: contentView) 29 | self.window = window 30 | window.makeKeyAndVisible() 31 | } 32 | } 33 | 34 | func sceneDidDisconnect(_ scene: UIScene) { 35 | // Called as the scene is being released by the system. 36 | // This occurs shortly after the scene enters the background, or when its session is discarded. 37 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 38 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 39 | } 40 | 41 | func sceneDidBecomeActive(_ scene: UIScene) { 42 | // Called when the scene has moved from an inactive state to an active state. 43 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 44 | } 45 | 46 | func sceneWillResignActive(_ scene: UIScene) { 47 | // Called when the scene will move from an active state to an inactive state. 48 | // This may occur due to temporary interruptions (ex. an incoming phone call). 49 | } 50 | 51 | func sceneWillEnterForeground(_ scene: UIScene) { 52 | // Called as the scene transitions from the background to the foreground. 53 | // Use this method to undo the changes made on entering the background. 54 | } 55 | 56 | func sceneDidEnterBackground(_ scene: UIScene) { 57 | // Called as the scene transitions from the foreground to the background. 58 | // Use this method to save data, release shared resources, and store enough scene-specific state information 59 | // to restore the scene back to its current state. 60 | } 61 | 62 | 63 | } 64 | 65 | -------------------------------------------------------------------------------- /CropImageDemo/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhxf2012/CropImageView/2beb09c5ff13bbb58c9134ecac6c594d91732486/CropImageDemo/demo.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 zhxf2012 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.2 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "CropImageView", 8 | platforms: [ .macOS(.v10_15), .iOS(.v13) ], 9 | products: [ 10 | // Products define the executables and libraries produced by a package, and make them visible to other packages. 11 | .library( 12 | name: "CropImageView", 13 | targets: ["CropImageView"]), 14 | ], 15 | dependencies: [ 16 | // Dependencies declare other packages that this package depends on. 17 | // .package(url: /* package url */, from: "1.0.0"), 18 | ], 19 | targets: [ 20 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 21 | // Targets can depend on other targets in this package, and on products in packages which this package depends on. 22 | .target( 23 | name: "CropImageView", 24 | dependencies: []), 25 | .testTarget( 26 | name: "CropImageViewTests", 27 | dependencies: ["CropImageView"]), 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CropImageView 2 | Crop Images in SwiftUI. 3 | 4 | A SwiftUI implementation of cropping images in selected areas. Here are some of its useful features: 5 | 6 | * Supports input with a UIImage and cropSize. 7 | * Supports dragging and dropping the input image 8 | * Supports scale for zooming in and out. 9 | * Supports saving the result UIImage 10 | * Supports showing in present and dismiss 11 | * Supports SwiftUI in iOS 13 and MacOS 10.15 12 | 13 | ### Pull requests and suggestions welcome :) 14 | Report Bug· Suggest a feature 15 | 16 | ## Getting Started 17 | To run the example project, clone the repo, and run. 18 | 19 | ## Requirements 20 | * Xcode 11.5 and above 21 | * iOS 13 or MacOS 10.15 and above 22 | 23 | 24 | ## Installation 25 | 26 | ### As Swift Package 27 | CropImageView is a *swift package*. 28 | * It can be imported into an app project using Xcode’s new Swift Packages option, which is located in the File menu. 29 | * When asked, use this repository's URL: https://github.com/zhxf2012/CropImageView 30 | 31 | Alternatively, if you're unable to use SPM for some reason, you can import it manually. 32 | 33 | ### Manual installation 34 | Add ` CropImageView.swift` to your project. 35 | 36 | ## Usage 37 | ### Simple use 38 | 39 | var inputImage: UIImage { 40 | return UIImage(named: "demo") ?? UIImage(systemName: "sun.haze.fill")! 41 | } 42 | 43 | var body: some View { 44 | VStack { 45 | Button(action: { 46 | self.showCropView = true 47 | }) { 48 | Text("Show the crop view") 49 | } 50 | 51 | if self.croppedImage != nil { 52 | Image(uiImage: self.cropedImage!) 53 | .resizable() 54 | .scaledToFit() 55 | .padding() 56 | } 57 | } 58 | .sheet(isPresented: $showCropView,onDismiss:finishedCrop ) { 59 | CropImageView(inputImage: self.inputImage, resultImage: self.$croppedImage, cropSize: CGSize(width: 250, height: 250)) 60 | } 61 | } 62 | 63 | func finishedCrop() { 64 | 65 | } 66 | 67 | 68 | ## Author 69 | 70 | Xingfa Zhou 71 | 72 | ## Contributor 73 | 74 | 75 | ## License 76 | 77 | CropImageView is available under the MIT license. See the LICENSE file for more info. 78 | -------------------------------------------------------------------------------- /Sources/CropImageView/CropImageView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CropImageView.swift 3 | // CropImageView 4 | // 5 | // Created by Xingfa Zhou on 2020/7/30. 6 | // Copyright © 2020 Yitesi. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | @available(iOS 13.0, OSX 10.15, *) 12 | struct RectHole: Shape { 13 | let holeSize: CGSize 14 | 15 | func path(in rect: CGRect) -> Path { 16 | let path = CGMutablePath() 17 | path.move(to: rect.origin) 18 | path.addLine(to: .init(x: rect.maxX, y: rect.minY)) 19 | path.addLine(to: .init(x: rect.maxX, y: rect.maxY)) 20 | path.addLine(to: .init(x: rect.minX, y: rect.maxY)) 21 | path.addLine(to: rect.origin) 22 | path.closeSubpath() 23 | let newRect = CGRect(origin: .init(x: rect.midX - holeSize.width / 2.0, 24 | y: rect.midY - holeSize.height / 2.0), 25 | size: holeSize) 26 | path.move(to: newRect.origin) 27 | path.addLine(to: .init(x: newRect.maxX, y: newRect.minY)) 28 | path.addLine(to: .init(x: newRect.maxX, y: newRect.maxY)) 29 | path.addLine(to: .init(x: newRect.minX, y: newRect.maxY)) 30 | path.addLine(to: newRect.origin) 31 | path.closeSubpath() 32 | return Path(path) 33 | } 34 | } 35 | 36 | @available(iOS 13.0, OSX 10.15, *) 37 | public struct CropImageView: View { 38 | @State private var dragAmount = CGSize.zero 39 | @State private var scale: CGFloat = 1.0 40 | @State private var clipped = false 41 | @State private var tempResult: UIImage? 42 | @State private var result: Image? 43 | 44 | @Binding var resultImage: UIImage? 45 | 46 | var cropSize: CGSize 47 | var inputImage: UIImage 48 | 49 | @Environment(\.presentationMode) var presentationMode 50 | 51 | /*Fix error by spm: 'CropImageView' initializer is inaccessible due to 'internal' protection level 52 | .Lesson learned: all public struct need a public init 53 | https://stackoverflow.com/questions/54673224/public-struct-in-framework-init-is-inaccessible-due-to-internal-protection-lev?rq=1 54 | .Maybe there is another way. 55 | */ 56 | public init(inputImage: UIImage, resultImage: Binding, cropSize: CGSize) { 57 | self.inputImage = inputImage 58 | _resultImage = resultImage 59 | self.cropSize = cropSize 60 | } 61 | 62 | var imageView: some View { 63 | Image(uiImage: inputImage) 64 | .resizable() 65 | .aspectRatio(contentMode: .fit) 66 | 67 | // 缩放 (Zoom) 68 | .gesture(MagnificationGesture() 69 | .onChanged { value in 70 | self.scale = value.magnitude 71 | } 72 | ) 73 | // 拖拽 (Drag and drop) 74 | .highPriorityGesture( 75 | DragGesture() 76 | .onChanged { value in 77 | self.dragAmount = value.translation 78 | } 79 | .onEnded { value in 80 | self.dragAmount = value.translation 81 | } 82 | ) 83 | 84 | // 点击放大 (Click to enlarge) 85 | .gesture( 86 | TapGesture() 87 | .onEnded { _ in 88 | self.scale += 0.1 89 | print("\(self.scale)") 90 | } 91 | ) 92 | } 93 | 94 | public var body: some View { 95 | GeometryReader { proxy in 96 | ZStack { 97 | ZStack { 98 | if self.clipped { 99 | self.result?.resizable().scaledToFit().frame(width: self.cropSize.width, 100 | height: self.cropSize.height) 101 | .overlay(Rectangle().stroke(Color.blue, lineWidth: 2)) 102 | } else { 103 | self.imageView 104 | .scaleEffect(self.scale) 105 | .offset(self.dragAmount) 106 | .animation(.easeInOut) 107 | // .scaledToFit() 108 | } 109 | 110 | RectHole(holeSize: self.cropSize) 111 | .fill(Color(UIColor.black.withAlphaComponent(0.5)), 112 | style: FillStyle(eoFill: true, antialiased: true)) 113 | .allowsHitTesting(false) 114 | Rectangle() 115 | .foregroundColor(Color.clear) 116 | .frame(width: self.cropSize.width, height: self.cropSize.height) 117 | .background(Rectangle().stroke(Color.white, lineWidth: 2)) 118 | } 119 | .frame(width: proxy.size.width, height: proxy.size.height) 120 | 121 | VStack { 122 | HStack { 123 | Button(action: { 124 | self.presentationMode.wrappedValue.dismiss() 125 | }) { 126 | Text("Cancel") 127 | .padding() 128 | } 129 | 130 | Spacer() 131 | 132 | Button(action: { 133 | if self.tempResult == nil { 134 | self.cropTheImageWithImageViewSize(proxy.size) 135 | } 136 | self.resultImage = self.tempResult 137 | self.presentationMode.wrappedValue.dismiss() 138 | }) { 139 | Text("Done") 140 | .padding() 141 | } 142 | } 143 | .background(Color.secondary) 144 | 145 | Spacer() 146 | 147 | HStack { 148 | Text("Size:\(Int(self.scale * 100))%") 149 | .frame(width: 90) 150 | .foregroundColor(.accentColor) 151 | .padding() 152 | 153 | Slider(value: self.$scale, in: 0.1 ... 2, step: 0.01) 154 | .frame(width: 140) 155 | .padding() 156 | 157 | Spacer() 158 | 159 | Button(action: { 160 | self.cropTheImageWithImageViewSize(proxy.size) 161 | self.clipped.toggle() 162 | 163 | }) { 164 | Image(systemName: self.clipped ? "gobackward" : "crop") 165 | } 166 | .padding() 167 | } 168 | .background(Color.secondary) 169 | 170 | } 171 | .accentColor(.accentColor) 172 | } 173 | } 174 | } 175 | 176 | func cropTheImageWithImageViewSize(_ size: CGSize) { 177 | 178 | let imsize = inputImage.size 179 | let scale = max(inputImage.size.width / size.width, 180 | inputImage.size.height / size.height) 181 | let zoomScale = self.scale 182 | // print("imageView size:\(size), image size:\(imsize), aspectScale:\(scale),zoomScale:\(zoomScale),currentPostion:\(dragAmount)") 183 | let currentPositionWidth = self.dragAmount.width * scale 184 | let currentPositionHeight = self.dragAmount.height * scale 185 | 186 | let croppedImsize = CGSize(width: (self.cropSize.width * scale) / zoomScale, height: (self.cropSize.height * scale) / zoomScale) 187 | 188 | let xOffset = (( imsize.width - croppedImsize.width) / 2.0) - (currentPositionWidth / zoomScale) 189 | let yOffset = (( imsize.height - croppedImsize.height) / 2.0) - (currentPositionHeight / zoomScale) 190 | let croppedImrect = CGRect(x: xOffset, y: yOffset, width: croppedImsize.width, height: croppedImsize.height) 191 | 192 | // print("croppedImsize:\(croppedImsize),croppedImrect:\(croppedImrect)") 193 | if let cropped = inputImage.cgImage?.cropping(to: croppedImrect) { 194 | let croppedIm = UIImage(cgImage: cropped) 195 | tempResult = croppedIm 196 | result = Image(uiImage: croppedIm) 197 | } 198 | } 199 | } 200 | 201 | struct CropImageView_Previews: PreviewProvider { 202 | @State static var result: UIImage? 203 | static var previews: some View { 204 | CropImageView(inputImage: UIImage(systemName: "sun.haze.fill")!, resultImage: self.$result, cropSize: .init(width: 200, height: 200)) 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /Tests/CropImageViewTests/CropImageViewTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import CropImageView 3 | 4 | final class CropImageViewTests: XCTestCase { 5 | func testExample() { 6 | // This is an example of a functional test case. 7 | // Use XCTAssert and related functions to verify your tests produce the correct 8 | // results. 9 | // XCTAssertEqual(CropImageView.self, CropImageView.Type) 10 | } 11 | 12 | static var allTests = [ 13 | ("testExample", testExample), 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /Tests/CropImageViewTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(CropImageViewTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import CropImageViewTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += CropImageViewTests.allTests() 7 | XCTMain(tests) 8 | --------------------------------------------------------------------------------