ImagePickerController函数内存泄漏问题

时间:2018-07-05 06:16:19

标签: swift xcode memory-leaks uiimagepickercontroller uiimagepickercontrollermediatype

我一般都不熟悉编程,因此遇到了以下代码专门涉及“ imagePickerController”的内存泄漏问题。仅在选择图像后关闭UIimagePickerController后才发生泄漏。感谢您为解决此问题提供的帮助。

let imagePicked = UIImagePickerController()

 @IBAction func addPhoto(_ sender: UIBarButtonItem) {
   let pickerController = UIImagePickerController()
    pickerController.delegate = self
    pickerController.allowsEditing = false

    imagePicked.delegate = self

    let alertController = UIAlertController(title: "Add New Image", message: "Choose From", preferredStyle: .actionSheet)

    let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
        pickerController.sourceType = .camera
        self.present(pickerController, animated: true, completion: nil)
    }

    let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
        pickerController.sourceType = .photoLibrary
        self.present(pickerController, animated: true, completion: nil)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)


    alertController.addAction(cameraAction)
    alertController.addAction(photosLibraryAction)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)


}



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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        var newImageView = UIImageView()
        newImageView = UIImageView(image: image)
        newImageView.contentMode = .scaleAspectFit
        newImageView.center = textView.center

        textView.addSubview(newImageView)

    }
    dismiss(animated: true, completion: nil)


}

编辑一个:

感谢您删除了@objc前面的didFinishPickingMediaWithInfo以及多余的UIImagePickerController对象,但是我仍然遇到同样的问题。我也尝试过picker.dismiss(animated: true, completion: nil),但这仍然不能解决问题。

是的,我相当确定这是内存泄漏错误,因为我已经运行了测试仪,并将照片从照片库或相机放入应用程序后,我得到了: picture 1 picture 2

此外,添加几张图片后,该应用程序将崩溃并显示“由于内存问题而终止”

0 个答案:

没有答案