未调用 UIImagePickerControllerDelegate 函数
UIImagePickerControllerDelegate functions are not being called
我有一个 class,我在其中将 UIImagePickerController
调用到 select 图像和 return 它以便我可以在我的代码中的其他地方使用它。
现在我设置了委托并实现了我需要的必要功能,但是没有调用委托函数。
我的代码部分有问题:
import UIKit
typealias PhotoTakingHelperCallback = (UIImage? -> Void)
class PhotoTakingHelper: NSObject
{
weak var viewController:UIViewController!
var callback: PhotoTakingHelperCallback
var imagePickerController: UIImagePickerController?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
showPhotoSourceSelection()
}
func showPhotoSourceSelection() {
let alertController = UIAlertController(title: nil, message: "Where do you want to get your picture from?", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let photoLibraryAction = UIAlertAction(title: "Photo from Library", style: .Default) { (action) -> Void in
self.showImagePickerController(.PhotoLibrary)
}
alertController.addAction(photoLibraryAction)
if (UIImagePickerController.isCameraDeviceAvailable(.Rear) ) {
let cameraAction = UIAlertAction(title: "Photo from Camera", style: .Default, handler: { (action) -> Void in
self.showImagePickerController(.Camera)
})
alertController.addAction(cameraAction)
}
viewController.presentViewController(alertController, animated: true, completion: nil)
}
func showImagePickerController(sourceType: UIImagePickerControllerSourceType) {
imagePickerController = UIImagePickerController()
imagePickerController!.delegate = self
imagePickerController!.sourceType = sourceType
print("Set the delegate \(imagePickerController?.delegate?.isMemberOfClass(PhotoTakingHelper))")
self.viewController.presentViewController(imagePickerController!, animated: true, completion: nil)
}
}
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("HELLO?")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
viewController.dismissViewControllerAnimated(false, completion: nil)
print("calling the callback now " )
callback(image)
print("Finished calling the callback")
}
}
我检查了 iPhone: UIImagePickerControllerDelegate methods not Invoked? and a similar problem 和更多,但 none 已经解决了这个问题。
我已经意识到 none 的委托方法被调用,因为当我 运行 通过模拟器上的程序并在照片库中选择一个图像时,控制台只打印。
Set the delegate Optional(true)
您正在创建一个 PhotoTakingHelper
但您没有保留对它的引用。也就是说,你正在做这样的事情:
// In your view controller
@IBAction func pickImageButtonWasTapped() {
_ = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
UIImagePickerController
仅保留对其委托的弱引用,这不足以使其保持活动状态,因此您必须保留对其的强引用。您可以通过将助手存储在视图控制器的实例变量中来实现,如下所示:
// In your view controller
var helper: PhotoTakingHelper?
@IBAction func pickImageButtonWasTapped() {
helper = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
或者您可以让助手保留对自身的引用,如下所示:
// In PhotoTakingHelper
var me: PhotoTakingHelper?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
me = self
showPhotoSourceSelection()
}
然后您需要在图像选择器完成时将引用设置为 nil,以释放助手:
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
viewController.dismissViewControllerAnimated(false, completion: nil)
callback(info[UIImagePickerControllerOriginalImage] as? UIImage)
me = nil
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
viewController.dismissViewControllerAnimated(false, completion: nil)
me = nil
}
}
我有一个 class,我在其中将 UIImagePickerController
调用到 select 图像和 return 它以便我可以在我的代码中的其他地方使用它。
现在我设置了委托并实现了我需要的必要功能,但是没有调用委托函数。
我的代码部分有问题:
import UIKit
typealias PhotoTakingHelperCallback = (UIImage? -> Void)
class PhotoTakingHelper: NSObject
{
weak var viewController:UIViewController!
var callback: PhotoTakingHelperCallback
var imagePickerController: UIImagePickerController?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
showPhotoSourceSelection()
}
func showPhotoSourceSelection() {
let alertController = UIAlertController(title: nil, message: "Where do you want to get your picture from?", preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let photoLibraryAction = UIAlertAction(title: "Photo from Library", style: .Default) { (action) -> Void in
self.showImagePickerController(.PhotoLibrary)
}
alertController.addAction(photoLibraryAction)
if (UIImagePickerController.isCameraDeviceAvailable(.Rear) ) {
let cameraAction = UIAlertAction(title: "Photo from Camera", style: .Default, handler: { (action) -> Void in
self.showImagePickerController(.Camera)
})
alertController.addAction(cameraAction)
}
viewController.presentViewController(alertController, animated: true, completion: nil)
}
func showImagePickerController(sourceType: UIImagePickerControllerSourceType) {
imagePickerController = UIImagePickerController()
imagePickerController!.delegate = self
imagePickerController!.sourceType = sourceType
print("Set the delegate \(imagePickerController?.delegate?.isMemberOfClass(PhotoTakingHelper))")
self.viewController.presentViewController(imagePickerController!, animated: true, completion: nil)
}
}
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("HELLO?")
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
viewController.dismissViewControllerAnimated(false, completion: nil)
print("calling the callback now " )
callback(image)
print("Finished calling the callback")
}
}
我检查了 iPhone: UIImagePickerControllerDelegate methods not Invoked? and a similar problem 和更多,但 none 已经解决了这个问题。
我已经意识到 none 的委托方法被调用,因为当我 运行 通过模拟器上的程序并在照片库中选择一个图像时,控制台只打印。
Set the delegate Optional(true)
您正在创建一个 PhotoTakingHelper
但您没有保留对它的引用。也就是说,你正在做这样的事情:
// In your view controller
@IBAction func pickImageButtonWasTapped() {
_ = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
UIImagePickerController
仅保留对其委托的弱引用,这不足以使其保持活动状态,因此您必须保留对其的强引用。您可以通过将助手存储在视图控制器的实例变量中来实现,如下所示:
// In your view controller
var helper: PhotoTakingHelper?
@IBAction func pickImageButtonWasTapped() {
helper = PhotoTakingHelper(viewController: self) { image in
print("Callback!")
}
}
或者您可以让助手保留对自身的引用,如下所示:
// In PhotoTakingHelper
var me: PhotoTakingHelper?
init(viewController:UIViewController , callback:PhotoTakingHelperCallback) {
self.viewController = viewController
self.callback = callback
super.init()
me = self
showPhotoSourceSelection()
}
然后您需要在图像选择器完成时将引用设置为 nil,以释放助手:
extension PhotoTakingHelper: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
viewController.dismissViewControllerAnimated(false, completion: nil)
callback(info[UIImagePickerControllerOriginalImage] as? UIImage)
me = nil
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
viewController.dismissViewControllerAnimated(false, completion: nil)
me = nil
}
}