我刚刚为我的目的创建了一个自定义类(将某些内容导出到目标)。
我的扩展课程如下
import Foundation
import UIKit
class PBExtensionExport: NSObject {
static let instance = PBExtensionExport()
fileprivate var progress: Int = 0
func exportPhotos(images: [UIImage], completion: (Bool) -> ()) {
for image in images {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(updateStatus), nil)
// Progress monitoring
completion(images.count == progress)
}
}
@objc private func updateStatus() {
// Updating progress status
progress += 1
}
}
这就是我在主要过程中调用它的方式:
PBExtensionExport.instance.exportPhotos(images: selectedImages, completion: { (success) -> Void in
if success {
// Do something
} else {
}
})
问题是没有编译错误。但是当我运行我的代码时,命令显示运行时异常:
***由于未捕获的异常终止应用程序' NSInvalidArgumentException', 原因:' - [NSInvocation setArgument:atIndex:]:index(2)超出范围[-1,1]'
有人可以帮我解释这个问题吗?
答案 0 :(得分:2)
根据docs,updateStatus
的{{1}}参数需要此方法签名时,UIImageWriteToSavedPhotosAlbum
不会参数。
completionSelector
在Swift中,这就像
- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo;
答案 1 :(得分:-1)
你应该使用Photos框架来保存图像。请参阅以下代码并正确使用完成处理程序。
fileprivate var currentImageIndex = 0
typealias imagecompletionBlock = (_ isSuccess: Bool) -> Void
var arrImages:[UIImage]?
var imageSuccessBlock : imagecompletionBlock?
func saveImageToLibrary(images:[UIImage],completion:@escaping ((Bool) -> Void))
{
self.imageSuccessBlock = completion
self.currentImageIndex = 0
}
func saveImage()
{
if self.currentImageIndex == self.arrImages?.count
{
self.imageSuccessBlock!(true)
}else{
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: self.arrImages![self.currentImageIndex])
}, completionHandler: { success, error in
DispatchQueue.main.async {
if success {
// Saved successfully!
self.saveImage()
}
else if let error = error {
// Save photo failed with error
}
else {
// Save photo failed with no error
}
}
})
}
}