UIImagePickerController内存泄漏问题,.camera很好,但不适用于.photoLibrary

时间:2018-11-21 09:55:49

标签: swift uiimagepickercontroller

UIImagePickerController内存泄漏问题,.camera很好,但不适用于.photoLibrary

代码如下:

func PickLibraryTapped() {
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
        let LibraryPhotoPicker = UIImagePickerController()
        LibraryPhotoPicker.sourceType = .photoLibrary
        LibraryPhotoPicker.delegate = self
        LibraryPhotoPicker.allowsEditing = false
        self.present(LibraryPhotoPicker, animated: true, completion: nil)
    }
    else
    {
        print("模拟其中无法打开照相机,请在真机中使用");
    }
}

代理方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
    let pickedImage = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as! UIImage
    picker.dismiss(animated: true, completion:nil)
}

错误: 设备弹出一个警报视图,指示“内存泄漏”和“对象已释放”(UIImagePickerController)

In case that UIImagePickerController should not be dealloced, override -willDealloc in UIImagePickerController by returning NO.

更新!!:

已解决的问题: 引发此问题是因为使用了“ MLeakFinder”窗格,在Unistall MLeakFinder之后,弹出警报消失了。代码没有错误。

提醒: 此代码中不再使用convertFromUIImagePickerControllerInfoKeyDictionary。 应该在下面尝试:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let pickedImage = info[.originalImage] as? UIImage {
        //Do something you need
    }
    self.dismiss(animated: true, completion: nil)
}

0 个答案:

没有答案