【发布时间】:2018-02-19 18:22:14
【问题描述】:
我有一个独立的类来使用 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 的强引用,它在调用委托方法之前自动释放。
【问题讨论】:
-
您如何使用
ImagePickerService?您是否实例化它并保持对它的强引用? -
您应该设置 imagePicker 导航控制器委托:
imagePicker.navigationController?.delegate = self -
@silicon_valley 这就是问题所在,谢谢!
标签: ios swift uiimagepickercontroller swift4