我有一个项目,它有许多使用imagePicker的viewControllers。每次,我都必须再次复制didFinishPickingMediaWithInfo,只更改一些代码。
然后我决定将UIImagePickerControllerDelegate和UINavigationControllerDelegate包装到我自己的协议中并扩展此协议以实现didFinishPickingMediaWithInfo。但是,根本没有调用didFinishPickingMediaWithInfo。所有其他部分都运行良好,图像选择器和摄像机视图显示良好,但在完成拾取后,未调用didFinish函数。
我在网上看到了一些建议,比如这个。他们使用具体类来扭曲两个协议,而不是接口。
https://gist.github.com/rpassis/4622291029cd12e4ce2b7585d3e62d15
我不知道为什么我的解决方案是错误的,有人可以告诉我我的代码错误的原因。也许我误解了协议和协议扩展的某些部分。顺便说一句,我发现了一个警告
非 - ' @ objc'方法 ' imagePickerController(_:didFinishPickingMediaWithInfo:)'才不是 满足' @ objc'的可选要求协议 ' UIImagePickerControllerDelegate'
另一种解释来自 Swift protocol implementing another @objc protocol
我的代码如下所示。
public protocol ImagePickerDelegate: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func successActionFromCamera(with localIdentifier: String, picker: UIImagePickerController)
func successActionFromPhotoLibrary(with imageURL: URL, picker: UIImagePickerController)
}
扩展到我的自定义委托
extension ImagePickerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//camera
if info[UIImagePickerControllerReferenceURL] == nil {
func savePhotoAndTakeAction() {
var imagePlaceholder:PHObjectPlaceholder!
DispatchQueue.global(qos: .default).async {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest.creationRequestForAsset(from: info[UIImagePickerControllerOriginalImage]! as! UIImage)
imagePlaceholder = request.placeholderForCreatedAsset!
}, completionHandler: { (success, error) -> Void in
DispatchQueue.main.async {
if success {
//image saved to photos library.
self.successActionFromCamera(with: imagePlaceholder.localIdentifier, picker: picker)
} else {
picker.dismiss(animated: true, completion: nil)
print(error!.localizedDescription)
}
picker.dismiss(animated: true, completion: nil)
}
})
}
}
switch PHPhotoLibrary.authorizationStatus() {
case .denied:
picker.dismiss(animated: false) { BasePhotoUtil.showAccessAlertController(false) }
return
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == .authorized) {
savePhotoAndTakeAction()
}
else {
DispatchQueue.main.async {
picker.dismiss(animated: false, completion: { BasePhotoUtil.showAccessAlertController(false) })
}
return
}
})
default:
break
}
savePhotoAndTakeAction()
} else {
//photo library
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
self.successActionFromPhotoLibrary(with: imageURL, picker: picker)
} else {
picker.dismiss(animated: true, completion: nil)
}
}
}
呈现ImagePicker的功能
private static func showImagePickerView(isCamera: Bool, currentVC: UIViewController) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = currentVC as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.allowsEditing = false
imagePicker.navigationBar.isTranslucent = false
if isCamera {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
} else {
BaseAlertUtil.showNoFunctionAlertController(title: "No Camera", message: "Sorry, this device has no camera")
}
} else {
imagePicker.sourceType = .photoLibrary
}
currentVC.present(imagePicker, animated: true) {
BaseThemeUtil.setStatusBarStyle(.default)
}
}
答案 0 :(得分:0)
这个问题很复杂。它实际上不是ImagePicker问题,更像是协议扩展问题。原因是swift协议无法扩展objc协议。
我想我已经在我的问题中给出了解决方案和解释。
解释:
Non-'@objc' method does not satisfy optional requirement of '@objc' protocol
解决方案:https://gist.github.com/rpassis/4622291029cd12e4ce2b7585d3e62d15
我将按照上述解决方案更改我的代码,如果您有任何新想法,请回复我。非常感谢。