Swift version: 5.6
iOS 13.0 introduced a new micro-framework called VisionKit, which is specifically designed to make it possible to scan documents like Notes does.
You can then Vision OCR to scan the text if you want, but by default VNDocumentCameraViewController
just gives you images of each page.
To get started:
VNDocumentCameraViewControllerDelegate
protocol so you can handle delegate callbacks.VNDocumentCameraViewController
, setting its delegate property to whatever should be notified when a scan completes.So, something like this:
let vc = VNDocumentCameraViewController()
vc.delegate = self
present(vc, animated: true)
Once the scan completes your delegate will get called with the document, like this:
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
print("Found \(scan.pageCount)")
for i in 0 ..< scan.pageCount {
let img = scan.imageOfPage(at: i)
// ... your code here
}
}
The result of imageOfPage(at:)
is a UIImage
, so you’ll need to replace “your code here” with whatever you want to do with your images.
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 13.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.