gpt4 book ai didi

ios - 未调用 UIImagePickerControllerDelegate

转载 作者:行者123 更新时间:2023-11-28 10:44:56 26 4
gpt4 key购买 nike

我有一个独立的类,可以使用 UIImagePickerController 从相机/照片库中获取图像。

我似乎无法弄清楚为什么没有调用 didFinishPickingMediaWithInfo 或 imagePickerControllerDidCancel 方法..?我意识到这个问题已被问过很多次,但我无法弄清楚...我知道所有方法名称都是正确的。

我已经在 info.plist 中正确设置了以下所有内容:

  • 隐私 - 媒体库使用说明
  • 隐私 - 照片库使用说明
  • 隐私 - 照片库使用说明

import UIKit

class ImagePickerService: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

fileprivate var completion: ((UIImage) -> ())
fileprivate var imagePicker: UIImagePickerController

init(completion: @escaping ((UIImage) -> ())) {
self.completion = completion
self.imagePicker = UIImagePickerController()
super.init()
imagePicker.delegate = self
}

func show(from viewController: UIViewController) {
showAlert(from: viewController)
}

fileprivate func showAlert(from viewController: UIViewController) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Take a photo", style: .default) { _ in
self.openCamera(from: viewController)
})
alert.addAction(UIAlertAction(title: "Choose from library", style: .default) { _ in
self.openGallery(from: viewController)
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
viewController.present(alert, animated: true, completion: nil)
}

// MARK: Image Picker

fileprivate func openCamera(from viewController: UIViewController) {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
viewController.showOKAlert(title: "Error!", message: "No camera available")
return
}
showImagePicker(withSource: .camera, from: viewController)
}

fileprivate func openGallery(from viewController: UIViewController) {
showImagePicker(withSource: .photoLibrary, from: viewController)
}

fileprivate func showImagePicker(withSource source: UIImagePickerControllerSourceType, from viewController: UIViewController) {
imagePicker.sourceType = source
viewController.present(imagePicker, animated: true, completion: nil)
}

}

extension ImagePickerService {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
return
}
completion(image)
picker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}

}

编辑:问题是我没有在实例化时创建对 ImagePickerService 的强引用,它在调用委托(delegate)方法之前自动释放。

最佳答案

问题出在下面的代码片段中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
return
}
completion(image)
picker.dismiss(animated: true, completion: nil)
}

您希望始终有经过编辑的图像,但您的照片库中的所有照片可能并非如此。当 info[UIImagePickerControllerEditedImage] 返回 nil 时,请尝试使用 UIImagePickerControllerOriginalImage 键。

如果一开始没有调用这些方法,您需要确保对 ImagePickerService 实例有强引用。

例如,这段代码有效:

class ViewController: UIViewController {

var picker: ImagePickerService?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak self] in
guard let `self` = self else { return }
self.picker = ImagePickerService() { (image) in
print(image)
}
self.picker?.show(from: self)
}
}

}

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

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