Created
March 3, 2021 08:36
-
-
Save prafullakumar/1898474b08ce4a54faef755d135e9fb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import VisionKit | |
final class ContentViewModel: NSObject, ObservableObject { | |
@Published var errorMessage: String? | |
@Published var imageArray: [UIImage] = [] | |
func getDocumentCameraViewController() -> VNDocumentCameraViewController { | |
let vc = VNDocumentCameraViewController() | |
vc.delegate = self | |
return vc | |
} | |
func removeImage(image: UIImage) { | |
imageArray.removeAll{$0 == image} | |
} | |
} | |
extension ContentViewModel: VNDocumentCameraViewControllerDelegate { | |
func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) { | |
controller.dismiss(animated: true, completion: nil) | |
} | |
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) { | |
errorMessage = error.localizedDescription | |
} | |
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) { | |
print("Did Finish With Scan.") | |
for i in 0..<scan.pageCount { | |
self.imageArray.append(scan.imageOfPage(at:i)) | |
} | |
controller.dismiss(animated: true, completion: nil) | |
} | |
} | |
struct ContentView: View { | |
@ObservedObject var viewModel: ContentViewModel | |
var body: some View { | |
NavigationView { | |
List { | |
if let error = viewModel.errorMessage { | |
Text(error) | |
} else { | |
ForEach(viewModel.imageArray, id: \.self) { image in | |
Image(uiImage: image) | |
.resizable() | |
.aspectRatio(contentMode: .fit).contextMenu { | |
Button { | |
let items = [image] | |
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil) | |
UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController?.present(ac, animated: true) | |
} label: { | |
Label("Share", systemImage: "square.and.arrow.up") | |
} | |
Divider() | |
Button { | |
viewModel.removeImage(image: image) | |
} label: { | |
Label("Delete", systemImage: "delete.left") | |
} | |
} | |
} | |
} | |
}.navigationTitle("Vinson kit Demo") | |
.navigationBarItems(leading: Button(action: { | |
let items = viewModel.imageArray | |
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil) | |
UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController?.present(ac, animated: true) | |
}, label: { | |
Text("Share All Images") | |
}).disabled(viewModel.imageArray.count == 0), trailing: Button(action: { | |
UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController?.present(viewModel.getDocumentCameraViewController(), animated: true, completion: nil) | |
}, label: { | |
Text("Scan New Doc") | |
})) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView(viewModel: ContentViewModel()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment