9

I am facing error App Terminated due to Memory Pressure when I capture some images using UIImagePickerController Camera.

I receive memory warnings first and then suddenly app crashes. This issue is in iOS 7 specifically as in iOS 6 it is working fine.

Does someone know why is this memory issue occuring in iOS 7 on using camera.

Note: I tried to minimize RAM usage because it may also be the reason for this memory pressure. But still getting warning.

jowie
  • 8,028
  • 8
  • 55
  • 94
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61

1 Answers1

5

I just posted this answer on a similar post (iOS 7 UIImagePicker preview black screen). Here's what I said:

About 5 months ago my team discovered a memory leak with UIImagePickerController. Each instantiation slowed down the app exponentially (i.e. first alloc-init had a 1 second delay, second had a 2 second delay, third had a 5 second delay). Eventually, we were having 30-60 delays (similar to what you're experiencing).

We resolved the issue by subclassing UIImagePickerController and making it a Singleton. That way it was only ever initialized once. Now our delay is minimal and we avoid the leak. If subclassing isn't an option, try a class property in your viewController and just lazy load it like so.

-(UIImagePickerController *)imagePicker{
    if(!_imagePicker){
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
    return _imagePicker;
}

Then you can just call it later like:

[self presentViewController:self.imagePicker animated:YES completion:nil];

From what I could tell, this is just an issue with the UIImagePickerController in iOS 7. Previous versions seems to be fine.

Community
  • 1
  • 1
ebandersen
  • 2,362
  • 26
  • 25
  • 1
    Fascinating stuff. - Note that your solution _assumes_ that the image picker will be used only for taking picture (camera), which is not necessarily the case. – matt Feb 25 '14 at 19:36
  • Good point. You can always change it anytime by saying, self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary. The idea is though that your UIImagePickerController is only initialized once. – ebandersen Feb 25 '14 at 19:52
  • I will test thoroughly but it seems to have solved the problem. – Camus Apr 14 '14 at 23:22