当委托不是 UIViewController 时不调用 UIImagePickerControllerDelegate 方法 [英] UIImagePickerControllerDelegate Methods Not Called When Delegate Not UIViewController
问题描述
我有一个包含多个图像的视图,每个图像都打算从设备的照片库中选择.
I have a view that contains several images, each intended to be selected from the device's photo library.
我遵循了食物追踪器教程中的一个示例,使用点按手势识别器,每个图像视图一个.
I followed an example from the Food Tracker tutorial, using Tap Gesture Recognizers, one for each image view.
我已将每个 UIImageView 的插座连接到我的视图控制器中.
I've wired an outlet for each UIImageView into my view controller.
选择图像时,我希望将设备照片库中的图像设置为所选 UIImageView 的图像属性.我想这样做的方法是使用封装到采用 UIImagePickerControllerDelegate 协议的类中的闭包.但是,此类上的方法永远不会被调用.如果我改为将我的视图控制器设为委托,则会正确调用这些方法(但我不知道要设置哪个图像).
When an image is selected, I want the image from the device's photo library to be set to the image property of the selected UIImageView. The way I thought to do this is to use closures encapsulated into a class adopting the UIImagePickerControllerDelegate protocol. However, the methods on this class are never called. If I instead make my view controller the delegate, the methods are correctly called (but I don't know which image to set).
这里有一些代码需要澄清:
Here's some code to clarify:
// Encapsulate callbacks for the selected image
class ImagePicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var select: (image: UIImage) -> Void
var cancel: () -> Void
init(select: (image: UIImage) -> Void, cancel: () -> Void) {
self.select = select
self.cancel = cancel
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
cancel()
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
select(selectedImage)
}
}
然后在我的视图控制器中,我将此方法连接到手势识别器:
Then within my view controller, I have wired this method to the gesture recognizer:
@IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
textField.resignFirstResponder()
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .PhotoLibrary
let imagePicker = ImagePicker(
select: {(image: UIImage) -> Void in
let imageView = sender.view as! UIImageView
imageView.image = image
self.dismissViewControllerAnimated(true, completion: nil)
},
cancel: {() -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}
)
imagePickerController.delegate = imagePicker
presentViewController(imagePickerController, animated: true, completion: nil)
}
永远不会调用这些方法.我在闭包和两个委托方法(imagePickerControllerDidCancel(_:)
和 imagePickerController(_:didFinishPickingMediaWithInfo:)
中设置了断点(并打印到控制台)>).
The methods are never called. I've set break points (and printed to the console) in both the closures and in the two delegate methods (imagePickerControllerDidCancel(_:)
and imagePickerController(_:didFinishPickingMediaWithInfo:)
).
还有什么我需要实施的吗?或者这只是错误的做法"?感谢您的帮助.
Is there something else I need to implement? Or is this just "the wrong way to do it"? Thanks for any help.
推荐答案
您的 ImagePicker
实例在 selectImageFromPhotoLibrary
函数结束时超出范围.因此图像选择器的代表不见了.您需要保持对 ImagePicker
的强引用.使用实例变量是一种可能的选择.
Your ImagePicker
instance goes out of scope at the end of the selectImageFromPhotoLibrary
function. Therefore the image picker's delegate is gone. You need to keep a strong reference to the ImagePicker
. Using an instance variable is a likely option.
这篇关于当委托不是 UIViewController 时不调用 UIImagePickerControllerDelegate 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!