didFinishPickingImage不会被触发

时间:2017-01-04 15:32:35

标签: ios swift swift3 uiimagepickercontroller

我有一个班级

class ActivitiesMainPageController: UICollectionViewController      ,UIImagePickerControllerDelegate, UINavigationControllerDelegate{

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    switch indexPath.item {

    case 0 :
        let takePictureInstance = takePictures()
        takePictureInstance.presentCamera()
    }

}

takePictures()是自定义类

class takePictures: UIViewController, UIImagePickerControllerDelegate, UIAlertViewDelegate, UINavigationControllerDelegate
{
   var imagePicker: UIImagePickerController! = UIImagePickerController()
  func presentCamera()
    {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
    {

        let objViewController =  UIApplication.shared.keyWindow?.rootViewController

        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.allowsEditing = false
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        objViewController?.present(imagePicker,animated: true,completion: nil)
   }
    else
    {
        // error msg
        print ("error displaying Camera")
         noCamera()
    }
}



  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    print(self.Tag + "image picker controller")

    self.dismiss(animated: true, completion: nil)
  }
}

didfinishpickingimage永远不会被触发。我没有看到印刷声明。在阅读了SO ere,后,我想我没有正确地做代理。你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

为什么要从takePictures类呈现UIImagePicker(应该是TakePictures)?我认为问题源于此问题,因为您当前所处的视图控制器不是呈现选择器的视图控制器。如果您将presentCamera()函数拉出并将其放入ActivitiesMainPageController并将其指定为< - 作为代理,并将方法签名切换为正确的:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

你应该有更好的运气。

答案 1 :(得分:1)

问题出在您的ActivitiesMainPageController课程中。这是一个内存管理问题。您创建takePictures类的本地实例(FYI - 类名称应以大写字母开头),然后调用presentCamera方法。然后takePictureInstance变量立即超出范围。这意味着它没有更多的引用,并且它被取消分配。

因此,此时,图像选择器(弱)委托变为nil。这就是从不调用图像选择器委托方法的原因。

有两种方法可以解决这个问题:

  1. 强烈引用takePictures类中的ActivitiesMainPageController实例(使用属性而不是局部变量)。但是这有一个问题就是不知道何时发布实例。
  2. takePictures类中执行一个小技巧,它在创建时保留对自身的强引用,然后在图像选择器被解除后释放该强引用。
  3. 选项2可以像这样实现:

    class takePictures: UIViewController, UIImagePickerControllerDelegate, UIAlertViewDelegate, UINavigationControllerDelegate
    {
        var strongSelf: takePictures?
        var imagePicker: UIImagePickerController! = UIImagePickerController()
    
        func presentCamera()
        {
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
            {
                let objViewController =  UIApplication.shared.keyWindow?.rootViewController
    
                imagePicker =  UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.allowsEditing = false
                imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                objViewController?.present(imagePicker,animated: true,completion: nil)
                strongSelf = self // keep me around
            }
            else
            {
                // error msg
                print ("error displaying Camera")
                noCamera()
            }
        }
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
            print(self.Tag + "image picker controller")
    
            self.dismiss(animated: true, completion: nil)
            strongSelf = nil // let me go
        }
    }