将图像保存到相机胶卷时应用程序崩溃
App Crashes When saving image to camera roll
当我尝试将扫描件保存到相机胶卷时,应用程序崩溃了
代码如下:
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
// Make sure the user scanned at least one page
guard scan.pageCount >= 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.
var arrImages = [UIImage]()
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = reloadedImage(originalImage)
arrImages.append(fixedImage)
}
controller.dismiss(animated: true)
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let docURL = documentDirectory.appendingPathComponent("Delete This")
if Filetype == 1 {
let data = createNewPDF(arrImage: arrImages)
do {
try data?.write(to: docURL, options: .completeFileProtection)
print("Success")
} catch(let error) {
print("error is \(error.localizedDescription)")
}
} else {
if Filetype == 2 {
if customjg == 68 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
if customjg == 69 {
let originalImage = scan.imageOfPage(at: 1)
let rere = self.resizeImagezz(image: originalImage, targetSize: CGSize(width: Widthv, height: Heightv))
let fixedImage = rere.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}else{
if Filetype == 3 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.pngData()
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}
}
}
文件类型是段控制开关盒。默认情况下,第一个选项是 JPEG。
它在崩溃之前甚至不要求相机胶卷访问权限(是的,我已经把它放在 info.plist 文件中)。
目前只有 PDF 可用。
但不同之处在于,安装在 iOS 14 Beta 上时一切正常。
请尽快帮我解决这个问题。
感谢您提前的帮助。
根据文档 - https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum,
我们应该实施completionSelector
。并且对于与自己相同的集合 completionTarget
。
实施 api 如下:
UIImageWriteToSavedPhotosAlbum(reloadedImage!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
然后在这个completionSelector中:
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
guard let error = error else {//success return}
// found error
print(error)
}
当我尝试将扫描件保存到相机胶卷时,应用程序崩溃了
代码如下:
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
// Make sure the user scanned at least one page
guard scan.pageCount >= 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.
var arrImages = [UIImage]()
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = reloadedImage(originalImage)
arrImages.append(fixedImage)
}
controller.dismiss(animated: true)
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let docURL = documentDirectory.appendingPathComponent("Delete This")
if Filetype == 1 {
let data = createNewPDF(arrImage: arrImages)
do {
try data?.write(to: docURL, options: .completeFileProtection)
print("Success")
} catch(let error) {
print("error is \(error.localizedDescription)")
}
} else {
if Filetype == 2 {
if customjg == 68 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
if customjg == 69 {
let originalImage = scan.imageOfPage(at: 1)
let rere = self.resizeImagezz(image: originalImage, targetSize: CGSize(width: Widthv, height: Heightv))
let fixedImage = rere.jpegData(compressionQuality: 0.7)
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}else{
if Filetype == 3 {
for i in 0...scan.pageCount-1 {
let originalImage = scan.imageOfPage(at: i)
let fixedImage = originalImage.pngData()
let reloadedImage = UIImage(data: fixedImage!)
UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
//arrImages.append(fixedImage)
}
}
}
}
}
文件类型是段控制开关盒。默认情况下,第一个选项是 JPEG。 它在崩溃之前甚至不要求相机胶卷访问权限(是的,我已经把它放在 info.plist 文件中)。
目前只有 PDF 可用。
但不同之处在于,安装在 iOS 14 Beta 上时一切正常。
请尽快帮我解决这个问题。
感谢您提前的帮助。
根据文档 - https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum,
我们应该实施completionSelector
。并且对于与自己相同的集合 completionTarget
。
实施 api 如下:
UIImageWriteToSavedPhotosAlbum(reloadedImage!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
然后在这个completionSelector中:
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
guard let error = error else {//success return}
// found error
print(error)
}