gpt4 book ai didi

ios - UIImagePickerControllerDelegate didFinishPickingMediaWithInfo 未调用

转载 作者:行者123 更新时间:2023-12-01 22:18:30 27 4
gpt4 key购买 nike

我有一个项目,其中有许多使用 imagePicker 的 View Controller 。每次,我都必须再次复制 didFinishPickingMediaWithInfo 并且只更改一些代码。

然后我决定把 UIImagePickerControllerDelegate 和 UINavigationControllerDelegate 包装成我自己的协议(protocol),并扩展这个协议(protocol)来实现 didFinishPickingMediaWithInfo。但是,根本没有调用 didFinishPickingMediaWithInfo。所有其他部分都运行良好,图像拾取器和相机 View 显示良好,但完成拾取后,没有调用 didFinish 函数。

我在网上看到了一些建议,比如这个。他们使用具体类来扭曲这两个协议(protocol),而不是接口(interface)。

https://gist.github.com/rpassis/4622291029cd12e4ce2b7585d3e62d15

我不知道为什么我的解决方案是错误的,有人可以告诉我我的代码错误的原因。也许我误解了协议(protocol)和协议(protocol)扩展的某些部分。顺便说一句,我发现一个警告是

Non-'@objc' method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' does not satisfy optional requirement of '@objc' protocol '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)
}

扩展我的自定义委托(delegate)
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)
}
}

最佳答案

这个问题是重复的。其实不是ImagePicker问题,更像是协议(protocol)扩展问题。原因是 swift 协议(protocol)不能扩展 objc 协议(protocol)。

我想我已经在我的问题中给出了解决方案和解释。

说明:

  • Swift protocol implementing another @objc protocol
  • Non-'@objc' method does not satisfy optional requirement of '@objc' protocol

  • 解决方案: https://gist.github.com/rpassis/4622291029cd12e4ce2b7585d3e62d15

    我将按照上面的解决方案更改我的代码,如果您有任何新想法,请回复我。非常感谢。

    关于ios - UIImagePickerControllerDelegate didFinishPickingMediaWithInfo 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49441953/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com