14

I've referred to this very good reference: https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more but I'm having some very serious issues. After I take a photo, I receive a memory warning. This is for the first photo I take, not the second or third.

I was wondering if it's because I've got a couple of small jpegs loaded from the application directory into scrolling views. The only solution I can think of is to unload everything in my mainview whilst the UIImagePicker is active, and reload everything again afterwards, but I'm not sure that's the correct solution and I'm not sure how to do that.

Does the UIImagePicker use up that much memory? I haven't even got as far as processing or displaying the image it takes yet. I get a memory warning, even if I throw the image away.

Any help appreciated.

Community
  • 1
  • 1
mac_55
  • 4,130
  • 7
  • 31
  • 40

3 Answers3

45

For all the people that are still looking for the actual answer and not a vague statement then look here. I noticed there are hundreds of answers such as "Handle your memory" but that doesn't answer anything. Hope this helps someone else out there...

Change the following

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

To the following so your modal view dismisses before setting your image...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissModalViewControllerAnimated:YES];
    [imageView setImage:image];
}
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • This is really a good idea. When I debug, I got the images right, but my imageView was missing it. But dismissing modal view early, work like charm. Thanks! 1 up! – karim Oct 14 '11 at 16:15
9

Yes, this happens. The thing to remember is that it's okay to get a memory warning, it doesn't mean you're a bad person, you just need to make sure that your application doesn't crash or get confused in response to the memory warning.

In particular, you need to understand that the default action of UIViewController is to unload its views if they're not visible, and they won't be visible if the full-screen image picker is showing.

David Maymudes
  • 5,664
  • 31
  • 34
  • Right, so if I understand correctly, I don't need to hide my view whilst the UIImagePicker is displayed, as the view controller does that already, and that I'm allowed to get a memory warning after an image is taken.. so long as I'm not doing anything exotic (such as displaying a full resolution image?) – mac_55 Dec 02 '09 at 17:34
  • if your app is using "too much" memory, and doesn't free "enough" of it when you get a low memory warning, the OS can potentially kill the app. there are no exact values as far as I know for "enough" and "too much". – David Maymudes Dec 02 '09 at 22:23
  • From this answer (http://stackoverflow.com/questions/457568/iphone-development-memory-limitation-for-iphone-application/457730#457730) it seems you get the warning at around 22MB of usage. Though Apple has not officially confirmed it. – Ben S Dec 03 '09 at 16:29
  • So what is the solution for this then? Is there a way to prevent the ViewController that presents the modal UIImagePickerController from unloading itself? – jocull Dec 03 '10 at 20:45
  • 1
    The ViewController won't get unloaded, just its views. – David Maymudes Dec 21 '10 at 08:04
1

Most likely you are using uneditted images, and they come back at full blown size of 1400x1300 which is huge and w ill crash your app, i suggest resizing the pictures to the iphone native 320x480 resolution, should fix your problem

Daniel
  • 22,363
  • 9
  • 64
  • 71
  • 1
    Hi, The images I'm displaying are far smaller than that, and I receive the memory warning after the camera takes the picture and before I have a chance to resize the image, so I'm not sure that this is the problem. – mac_55 Dec 02 '09 at 16:42
  • yea it sounds that you might have other memory leaks somewhere t here – Daniel Dec 02 '09 at 16:54
  • so you take the picture and even before you can select it its c rashing? – Daniel Dec 02 '09 at 16:55
  • 1
    Yeah, I'm getting a warning before I press 'Use' and the the picker is hidden. – mac_55 Dec 02 '09 at 16:56
  • 2
    I get the message as soon as the picker is displayed, even before a picture is taken. – RyeMAC3 Jul 06 '11 at 21:45
  • mac_55 is right, in my research , there is 2MB of memory consuming at take picture. before we get the option to resize and save. i am searching for other info how to flush this. any one have idea on this? – Ganesh Dec 20 '11 at 07:06