未调用UIImagePickerControllerDelegate函数

时间:2015-10-29 04:12:58

标签: ios iphone swift uiimagepickercontroller

我有一个类,我在调用UIImagePickerController来选择图像并将其返回,以便我可以在我的代码中的其他地方使用它。

现在我设置了委托并实现了我需要的必要功能,但委托函数没有被调用。

我的代码中有问题的部分:

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?a similar problem等等,但没有人解决了这个问题。

我已经意识到没有调用任何委托方法,因为当我在模拟器上运行程序并在照片库中选择一个图像时,控制台只会打印。

  

设置委托可选(true)

1 个答案:

答案 0 :(得分:7)

您正在创建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
    }

}