Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController leaking memory after launch and during taking a picture. Makes app crash after taking more than a 100 pictures

I have been struggling with this issue for weeks now. I have look online all over the place and found nothing close to this issue other than this 2 outsider links:

https://github.com/cordova-plugin-camera-preview/cordova-plugin-camera-preview/issues/200

https://issues.apache.org/jira/browse/CB-11784

But those are not using the same environment and there is no actual solution to the problem.

Here is a screenshot of my Instrumentals using Xcode 8.3 right after I launch the imagePicker and take a picture:

enter image description here

Here is the code related to the imagePicker:

      //initializer on my class
      var imagePicker = UIImagePickerController()

      //imagepicker setup on ViewDidLoad()

      imagePicker.delegate = self
      imagePicker.allowsEditing = false
      imagePicker.mediaTypes = [kUTTypeImage as String]

      if UIImagePickerController.isSourceTypeAvailable(.camera) {
          imagePicker.sourceType = .camera

      }
      else {
        print("Sorry this app only supports camera")
      }


      //function to start picker when click on button
      func startPicker(){

        self.present(imagePicker, animated: false, completion: nil)

      }

       //delegate functions

      func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
      {
                imagePicker.dismiss(animated: false, completion: nil)
      }

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

           //display photo on view

           imagePicker.dismiss(animated: false, completion: nil)

      }

Is this a bug on IOS or is there a away to get rid off this memory leak?

like image 490
Fidel Avatar asked Oct 18 '22 09:10

Fidel


2 Answers

I had this problem as well. It appears to be a known bug in UIImagePickerController.

I created a minimal repro app here: https://github.com/davidgoli/UIImagePickerLeakTest

I solved it by implementing my own camera controller using https://github.com/imaginary-cloud/CameraManager.

like image 41
davidgoli Avatar answered Oct 21 '22 08:10

davidgoli


I just want to point out that as of IOS 12 this problem still exists and I had to use the Camera of AVFoundation to avoid the crashing. With AVFoundation I can take hundreds of pictures and no memory leak occurred.

like image 143
Fidel Avatar answered Oct 21 '22 08:10

Fidel