【发布时间】:2016-09-25 02:50:00
【问题描述】:
表面上我认为这一定是代理问题,但在要求代理后,正确的人被退回了。
我创建了一个 ImagePicker 类来处理所有 UIImagePickerController 的东西。在需要调用委托方法之前,一切都有效。在我选择一张照片后,imagePicker 将关闭,但 didFinishPickingMediaWithInfo 方法永远不会被调用。请帮忙!谢谢:)
func selectPhoto() {
imagePicker.delegate = self //Delegate gets set here
let photoAsk = UIAlertController.init( //Ask user if they want to take picture or choose one
title: "Edit Profile Picture",
message: nil,
preferredStyle: .alert)
let cameraAction = UIAlertAction.init(
title: "Take Photo",
style: .default) { (UIAlertAction) in
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
self.imagePicker.sourceType = .camera
UIApplication.topViewController()!.present(self.imagePicker, animated: true, completion:nil)
} else {
print("Cannot access camera in simulator.")
return
}
}
let photoLibraryAction = UIAlertAction.init(
title: "Photo Library",
style: .default) { (UIAlertAction) in
self.imagePicker.sourceType = .photoLibrary
UIApplication.topViewController()!.present(self.imagePicker, animated: true, completion:nil)
print("UIImagePickerDelegate: \(self.imagePicker.delegate.debugDescription)") // <--THIS PRINTS OUT "AppName.ImagePicker: 0x145d7bdf0>", and the class name is ImagePicker
}
let cancelAction = UIAlertAction.init(
title: "Cancel",
style: .cancel) { (UIAlertAction) in return }
photoAsk.addAction(cameraAction)
photoAsk.addAction(photoLibraryAction)
photoAsk.addAction(cancelAction)
imagePicker.mediaTypes = [kUTTypeImage as String]
UIApplication.topViewController()?.present(photoAsk, animated: true, completion: nil)
}
}
这永远不会被调用:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Image picked.") //NEVER PRINTS
}
【问题讨论】:
-
尝试设置
imagePicker.delegate = selfinviewDidLoadinstead -
@RashwanL 是一个自定义类,但是我试着把它放在类的
init方法中,并没有什么区别。 -
我很困惑。
selectPhoto()在哪里被调用? -
在您的问题中发布的这个自定义类的实例是否有强烈的参考?
-
@ClaytonAndrewCohn 你有什么解决办法吗?我在 ImageHelper 类中遇到了同样的问题。
标签: ios swift uiimagepickercontroller swift3