From 6edf0bef48b2c157fe974411a01c090aed20ead1 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 15 Jun 2019 21:38:53 +0800 Subject: [PATCH] 2019-06-15-ScanningDocuments --- .../Podfile | 0 .../Podfile.lock | 0 .../README.md | 0 .../project.pbxproj | 0 .../contents.xcworkspacedata | 0 .../xcshareddata/IDEWorkspaceChecks.plist | 0 .../xcschemes/ScanningDocuments.xcscheme | 0 .../contents.xcworkspacedata | 0 .../xcshareddata/IDEWorkspaceChecks.plist | 0 .../ScanningDocuments/AppDelegate.swift | 0 .../AppIcon.appiconset/Contents.json | 0 .../Assets.xcassets/Contents.json | 0 .../Base.lproj/LaunchScreen.storyboard | 0 .../Base.lproj/Main.storyboard | 0 .../ScanningDocuments/Info.plist | 0 .../ScanningDocuments/ViewController.swift | 0 2019-06-15-ScanningDocuments/Podfile | 6 + 2019-06-15-ScanningDocuments/Podfile.lock | 16 + 2019-06-15-ScanningDocuments/README.md | 1 + .../project.pbxproj | 351 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../xcschemes/ScanningDocuments.xcscheme | 92 +++++ .../ScanningDocuments/AppDelegate.swift | 21 ++ .../AppIcon.appiconset/Contents.json | 98 +++++ .../Assets.xcassets/Contents.json | 6 + .../Base.lproj/LaunchScreen.storyboard | 26 ++ .../Base.lproj/Main.storyboard | 105 ++++++ .../BoundingBoxImageView.swift | 79 ++++ .../ScanningDocuments/Info.plist | 47 +++ .../UIImageView-ImageRect.swift | 31 ++ .../ScanningDocuments/ViewController.swift | 167 +++++++++ 32 files changed, 1061 insertions(+) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/Podfile (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/Podfile.lock (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/README.md (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcodeproj/project.pbxproj (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcworkspace/contents.xcworkspacedata (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/AppDelegate.swift (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/Assets.xcassets/Contents.json (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/Base.lproj/LaunchScreen.storyboard (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/Base.lproj/Main.storyboard (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/Info.plist (100%) rename {2019-02-24-ScanningDocuments => 2019-02-24-ScanningDocumentsOld}/ScanningDocuments/ViewController.swift (100%) create mode 100644 2019-06-15-ScanningDocuments/Podfile create mode 100644 2019-06-15-ScanningDocuments/Podfile.lock create mode 100644 2019-06-15-ScanningDocuments/README.md create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/AppDelegate.swift create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/BoundingBoxImageView.swift create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/Info.plist create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/UIImageView-ImageRect.swift create mode 100644 2019-06-15-ScanningDocuments/ScanningDocuments/ViewController.swift diff --git a/2019-02-24-ScanningDocuments/Podfile b/2019-02-24-ScanningDocumentsOld/Podfile similarity index 100% rename from 2019-02-24-ScanningDocuments/Podfile rename to 2019-02-24-ScanningDocumentsOld/Podfile diff --git a/2019-02-24-ScanningDocuments/Podfile.lock b/2019-02-24-ScanningDocumentsOld/Podfile.lock similarity index 100% rename from 2019-02-24-ScanningDocuments/Podfile.lock rename to 2019-02-24-ScanningDocumentsOld/Podfile.lock diff --git a/2019-02-24-ScanningDocuments/README.md b/2019-02-24-ScanningDocumentsOld/README.md similarity index 100% rename from 2019-02-24-ScanningDocuments/README.md rename to 2019-02-24-ScanningDocumentsOld/README.md diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.pbxproj similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.pbxproj diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcworkspace/contents.xcworkspacedata b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcworkspace/contents.xcworkspacedata similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcworkspace/contents.xcworkspacedata rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcworkspace/contents.xcworkspacedata diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/AppDelegate.swift b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/AppDelegate.swift similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/AppDelegate.swift rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/AppDelegate.swift diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/Assets.xcassets/Contents.json similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/Assets.xcassets/Contents.json diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/Base.lproj/LaunchScreen.storyboard similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/Base.lproj/LaunchScreen.storyboard diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/Base.lproj/Main.storyboard similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/Base.lproj/Main.storyboard diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/Info.plist b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/Info.plist similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/Info.plist rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/Info.plist diff --git a/2019-02-24-ScanningDocuments/ScanningDocuments/ViewController.swift b/2019-02-24-ScanningDocumentsOld/ScanningDocuments/ViewController.swift similarity index 100% rename from 2019-02-24-ScanningDocuments/ScanningDocuments/ViewController.swift rename to 2019-02-24-ScanningDocumentsOld/ScanningDocuments/ViewController.swift diff --git a/2019-06-15-ScanningDocuments/Podfile b/2019-06-15-ScanningDocuments/Podfile new file mode 100644 index 0000000..0f18b92 --- /dev/null +++ b/2019-06-15-ScanningDocuments/Podfile @@ -0,0 +1,6 @@ +platform :ios, '12.1' +use_frameworks! + +target 'ScanningDocuments' do + pod 'WeScan', '>= 0.9' +end \ No newline at end of file diff --git a/2019-06-15-ScanningDocuments/Podfile.lock b/2019-06-15-ScanningDocuments/Podfile.lock new file mode 100644 index 0000000..54b7408 --- /dev/null +++ b/2019-06-15-ScanningDocuments/Podfile.lock @@ -0,0 +1,16 @@ +PODS: + - WeScan (1.0.0) + +DEPENDENCIES: + - WeScan (>= 0.9) + +SPEC REPOS: + https://github.com/cocoapods/specs.git: + - WeScan + +SPEC CHECKSUMS: + WeScan: a049a4a2a0eb1c4560cf649ebd933d07b0d9ef4c + +PODFILE CHECKSUM: d22881a0e2e5c28330e9b6d70931cec3d7a6d079 + +COCOAPODS: 1.6.0 diff --git a/2019-06-15-ScanningDocuments/README.md b/2019-06-15-ScanningDocuments/README.md new file mode 100644 index 0000000..7b2ebbe --- /dev/null +++ b/2019-06-15-ScanningDocuments/README.md @@ -0,0 +1 @@ +This is the example code for my blog post, which is available here: [https://schiavo.me/2019/scanning-documents/](https://schiavo.me/2019/scanning-documents/) diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj new file mode 100644 index 0000000..c96681c --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.pbxproj @@ -0,0 +1,351 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + B921C53322B4CF6F00B3D28D /* BoundingBoxImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B921C53222B4CF6F00B3D28D /* BoundingBoxImageView.swift */; }; + B921C53522B4DC2F00B3D28D /* UIImageView-ImageRect.swift in Sources */ = {isa = PBXBuildFile; fileRef = B921C53422B4DC2F00B3D28D /* UIImageView-ImageRect.swift */; }; + B9945CD42220215F00D0D511 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9945CD32220215F00D0D511 /* AppDelegate.swift */; }; + B9945CD62220215F00D0D511 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9945CD52220215F00D0D511 /* ViewController.swift */; }; + B9945CD92220215F00D0D511 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B9945CD72220215F00D0D511 /* Main.storyboard */; }; + B9945CDB2220216100D0D511 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B9945CDA2220216100D0D511 /* Assets.xcassets */; }; + B9945CDE2220216100D0D511 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B9945CDC2220216100D0D511 /* LaunchScreen.storyboard */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + B921C53222B4CF6F00B3D28D /* BoundingBoxImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoundingBoxImageView.swift; sourceTree = ""; }; + B921C53422B4DC2F00B3D28D /* UIImageView-ImageRect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView-ImageRect.swift"; sourceTree = ""; }; + B9945CD02220215F00D0D511 /* ScanningDocuments.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ScanningDocuments.app; sourceTree = BUILT_PRODUCTS_DIR; }; + B9945CD32220215F00D0D511 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + B9945CD52220215F00D0D511 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + B9945CD82220215F00D0D511 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + B9945CDA2220216100D0D511 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + B9945CDD2220216100D0D511 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + B9945CDF2220216100D0D511 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + B9945CCD2220215F00D0D511 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + B9945CC72220215F00D0D511 = { + isa = PBXGroup; + children = ( + B9945CD22220215F00D0D511 /* ScanningDocuments */, + B9945CD12220215F00D0D511 /* Products */, + ); + sourceTree = ""; + }; + B9945CD12220215F00D0D511 /* Products */ = { + isa = PBXGroup; + children = ( + B9945CD02220215F00D0D511 /* ScanningDocuments.app */, + ); + name = Products; + sourceTree = ""; + }; + B9945CD22220215F00D0D511 /* ScanningDocuments */ = { + isa = PBXGroup; + children = ( + B9945CD32220215F00D0D511 /* AppDelegate.swift */, + B9945CD52220215F00D0D511 /* ViewController.swift */, + B921C53222B4CF6F00B3D28D /* BoundingBoxImageView.swift */, + B921C53422B4DC2F00B3D28D /* UIImageView-ImageRect.swift */, + B9945CD72220215F00D0D511 /* Main.storyboard */, + B9945CDA2220216100D0D511 /* Assets.xcassets */, + B9945CDC2220216100D0D511 /* LaunchScreen.storyboard */, + B9945CDF2220216100D0D511 /* Info.plist */, + ); + path = ScanningDocuments; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + B9945CCF2220215F00D0D511 /* ScanningDocuments */ = { + isa = PBXNativeTarget; + buildConfigurationList = B9945CE22220216100D0D511 /* Build configuration list for PBXNativeTarget "ScanningDocuments" */; + buildPhases = ( + B9945CCC2220215F00D0D511 /* Sources */, + B9945CCD2220215F00D0D511 /* Frameworks */, + B9945CCE2220215F00D0D511 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = ScanningDocuments; + productName = ScanningDocuments; + productReference = B9945CD02220215F00D0D511 /* ScanningDocuments.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + B9945CC82220215F00D0D511 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1010; + LastUpgradeCheck = 1010; + ORGANIZATIONNAME = "Julian Schiavo"; + TargetAttributes = { + B9945CCF2220215F00D0D511 = { + CreatedOnToolsVersion = 10.1; + }; + }; + }; + buildConfigurationList = B9945CCB2220215F00D0D511 /* Build configuration list for PBXProject "ScanningDocuments" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = B9945CC72220215F00D0D511; + productRefGroup = B9945CD12220215F00D0D511 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + B9945CCF2220215F00D0D511 /* ScanningDocuments */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + B9945CCE2220215F00D0D511 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B9945CDE2220216100D0D511 /* LaunchScreen.storyboard in Resources */, + B9945CDB2220216100D0D511 /* Assets.xcassets in Resources */, + B9945CD92220215F00D0D511 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + B9945CCC2220215F00D0D511 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B921C53322B4CF6F00B3D28D /* BoundingBoxImageView.swift in Sources */, + B921C53522B4DC2F00B3D28D /* UIImageView-ImageRect.swift in Sources */, + B9945CD62220215F00D0D511 /* ViewController.swift in Sources */, + B9945CD42220215F00D0D511 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + B9945CD72220215F00D0D511 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + B9945CD82220215F00D0D511 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + B9945CDC2220216100D0D511 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + B9945CDD2220216100D0D511 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + B9945CE02220216100D0D511 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + B9945CE12220216100D0D511 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + B9945CE32220216100D0D511 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 74N8K5J2N7; + INFOPLIST_FILE = ScanningDocuments/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.julianschiavo.ScanningDocuments; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = 1; + }; + name = Debug; + }; + B9945CE42220216100D0D511 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 74N8K5J2N7; + INFOPLIST_FILE = ScanningDocuments/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.julianschiavo.ScanningDocuments; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.2; + TARGETED_DEVICE_FAMILY = 1; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + B9945CCB2220215F00D0D511 /* Build configuration list for PBXProject "ScanningDocuments" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B9945CE02220216100D0D511 /* Debug */, + B9945CE12220216100D0D511 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + B9945CE22220216100D0D511 /* Build configuration list for PBXNativeTarget "ScanningDocuments" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B9945CE32220216100D0D511 /* Debug */, + B9945CE42220216100D0D511 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = B9945CC82220215F00D0D511 /* Project object */; +} diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..2cb9cc3 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme new file mode 100644 index 0000000..7a6779c --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments.xcodeproj/xcshareddata/xcschemes/ScanningDocuments.xcscheme @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/AppDelegate.swift b/2019-06-15-ScanningDocuments/ScanningDocuments/AppDelegate.swift new file mode 100644 index 0000000..7178a78 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/AppDelegate.swift @@ -0,0 +1,21 @@ +// +// AppDelegate.swift +// ScanningDocuments +// +// Created by Julian Schiavo on 22/2/2019. +// Copyright © 2019 Julian Schiavo. All rights reserved. +// + +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } +} + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json b/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..d8db8d6 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json b/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard b/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..8ca4785 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard b/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard new file mode 100644 index 0000000..5dfa415 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/Base.lproj/Main.storyboard @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tap Scan to scan a document, which will be perspective corrected and enhanced. The output of performing text recognition on the document will be shown here. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/BoundingBoxImageView.swift b/2019-06-15-ScanningDocuments/ScanningDocuments/BoundingBoxImageView.swift new file mode 100644 index 0000000..a7ca18c --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/BoundingBoxImageView.swift @@ -0,0 +1,79 @@ +// +// TextAnnotationLayer.swift +// ScanningDocuments +// +// Created by Julian Schiavo on 15/6/2019. +// Copyright © 2019 Julian Schiavo. All rights reserved. +// + +import Vision +import UIKit + +/// Custom `UIImageView` subclass that adds support for displaying bounding boxes around detected text +class BoundingBoxImageView: UIImageView { + + /// The bounding boxes currently shown + private var boundingBoxViews = [UIView]() + + func load(boundingBoxes: [CGRect]) { + // Remove all the old bounding boxes before adding the new ones + removeExistingBoundingBoxes() + + // Add each bounding box + for box in boundingBoxes { + load(boundingBox: box) + } + } + + /// Removes all existing bounding boxes + func removeExistingBoundingBoxes() { + for view in boundingBoxViews { + view.removeFromSuperview() + } + boundingBoxViews.removeAll() + } + + private func load(boundingBox: CGRect) { + // Cache the image rectangle to avoid unneccessary work + let imageRect = self.imageRect + + // Create a mutable copy of the bounding box + var boundingBox = boundingBox + + // Flip the Y axis of the bounding box because Vision uses a different coordinate system to that of UIKit + boundingBox.origin.y = 1 - boundingBox.origin.y + + // Convert the bounding box rect based on the image rectangle + var convertedBoundingBox = VNImageRectForNormalizedRect(boundingBox, Int(imageRect.width), Int(imageRect.height)) + + // Adjust the bounding box based on the position of the image inside the UIImageView + // Note that we only adjust the axis that is not the same in both--because we're using `scaleAspectFit`, one of the axis will always be equal + if frame.width - imageRect.width != 0 { + convertedBoundingBox.origin.x += imageRect.origin.x + convertedBoundingBox.origin.y -= convertedBoundingBox.height + } else if frame.height - imageRect.height != 0 { + convertedBoundingBox.origin.y += imageRect.origin.y + convertedBoundingBox.origin.y -= convertedBoundingBox.height + } + + // Enlarge the bounding box to make it contain the text neatly + let enlargementAmount = CGFloat(2.2) + convertedBoundingBox.origin.x -= enlargementAmount + convertedBoundingBox.origin.y -= enlargementAmount + convertedBoundingBox.size.width += enlargementAmount * 2 + convertedBoundingBox.size.height += enlargementAmount * 2 + + // Create a view with a narrow border and transparent background as the bounding box + let view = UIView(frame: convertedBoundingBox) + view.layer.opacity = 1 + view.layer.borderColor = UIColor.orange.cgColor + view.layer.borderWidth = 2 + view.layer.cornerRadius = 2 + view.layer.masksToBounds = true + view.backgroundColor = .clear + + addSubview(view) + boundingBoxViews.append(view) + } +} + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/Info.plist b/2019-06-15-ScanningDocuments/ScanningDocuments/Info.plist new file mode 100644 index 0000000..256a613 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/Info.plist @@ -0,0 +1,47 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleDisplayName + Scanner + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + NSCameraUsageDescription + Used to scan documents. + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/UIImageView-ImageRect.swift b/2019-06-15-ScanningDocuments/ScanningDocuments/UIImageView-ImageRect.swift new file mode 100644 index 0000000..c4be21b --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/UIImageView-ImageRect.swift @@ -0,0 +1,31 @@ +// +// UIImageView-ImageSize.swift +// ScanningDocuments +// +// Created by Julian Schiavo on 15/6/2019. +// Copyright © 2019 Julian Schiavo. All rights reserved. +// + +import UIKit + +extension UIImageView { + /// Calculates the rect of an image displayed in a `UIImageView` with the `scaleAspectFit` `contentMode` + var imageRect: CGRect { + guard let image = image else { return bounds } + guard contentMode == .scaleAspectFit else { return bounds } + guard image.size.width > 0 && image.size.height > 0 else { return bounds } + + let scale: CGFloat + if image.size.width > image.size.height { + scale = bounds.width / image.size.width + } else { + scale = bounds.height / image.size.height + } + + let size = CGSize(width: image.size.width * scale, height: image.size.height * scale) + let x = (bounds.width - size.width) / 2.0 + let y = (bounds.height - size.height) / 2.0 + + return CGRect(x: x, y: y, width: size.width, height: size.height) + } +} diff --git a/2019-06-15-ScanningDocuments/ScanningDocuments/ViewController.swift b/2019-06-15-ScanningDocuments/ScanningDocuments/ViewController.swift new file mode 100644 index 0000000..a9e5511 --- /dev/null +++ b/2019-06-15-ScanningDocuments/ScanningDocuments/ViewController.swift @@ -0,0 +1,167 @@ +// +// ViewController.swift +// ScanningDocuments +// +// Created by Julian Schiavo on 22/2/2019. +// Copyright © 2019 Julian Schiavo. All rights reserved. +// + +import UIKit +import Vision +import VisionKit + +class ViewController: UIViewController, VNDocumentCameraViewControllerDelegate { + + @IBOutlet var imageView: BoundingBoxImageView! + @IBOutlet var textView: UITextView! + @IBOutlet var progressIndicator: UIProgressView! + @IBOutlet var scanButton: UIButton! + + var textRecognitionRequest = VNRecognizeTextRequest(completionHandler: nil) + private let textRecognitionWorkQueue = DispatchQueue(label: "TextRecognitionQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem) + + override func viewDidLoad() { + super.viewDidLoad() + + textView.textContainerInset = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 8) + textView.layer.cornerRadius = 10.0 + + imageView.layer.cornerRadius = 10.0 + scanButton.layer.cornerRadius = 10.0 + + scanButton.addTarget(self, action: #selector(scanDocument), for: .touchUpInside) + + setupVision() + } + + /// Setup the Vision request as it can be reused + private func setupVision() { + textRecognitionRequest = VNRecognizeTextRequest { (request, error) in + guard let observations = request.results as? [VNRecognizedTextObservation] else { return } + + var detectedText = "" + var boundingBoxes = [CGRect]() + for observation in observations { + guard let topCandidate = observation.topCandidates(1).first else { return } + + detectedText += topCandidate.string + detectedText += "\n" + + do { + guard let rectangle = try topCandidate.boundingBox(for: topCandidate.string.startIndex..= 1 else { + // You are responsible for dismissing the VNDocumentCameraViewController. + controller.dismiss(animated: true) + return + } + + // This is a workaround for the VisionKit bug which breaks the `UIImage` returned from `VisionKit` + // See the `Image Loading Hack` section below for more information. + let originalImage = scan.imageOfPage(at: 0) + let fixedImage = reloadedImage(originalImage) + + // You are responsible for dismissing the VNDocumentCameraViewController. + controller.dismiss(animated: true) + + // Process the image + processImage(fixedImage) + } + + func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) { + // The VNDocumentCameraViewController failed with an error. + // For now, we'll print it, but you should handle it appropriately in your app. + print(error) + + // You are responsible for dismissing the VNDocumentCameraViewController. + controller.dismiss(animated: true) + } + + func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) { + // You are responsible for dismissing the VNDocumentCameraViewController. + controller.dismiss(animated: true) + } + + // MARK: - Image Loading Hack + + /// VisionKit currently has a bug where the images returned reference unique files on disk which are deleted after dismissing the VNDocumentCameraViewController. + /// To work around this, we have to create a new UIImage from the data of the original image from VisionKit. + /// I have filed a bug (FB6156927) - hopefully this is fixed soon. + + func reloadedImage(_ originalImage: UIImage) -> UIImage { + guard let imageData = originalImage.jpegData(compressionQuality: 1), + let reloadedImage = UIImage(data: imageData) else { + return originalImage + } + return reloadedImage + } +}