imagepicker is deallocated in the dealloc function of my parent view controller.
Now, after a person takes a photo I do the following:
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//we need to save each image and then show it in the interface!
int row = (self.numPhotos / 4);
int mod = self.numPhotos % 4;
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setFrame:CGRectMake(mod * 80.0, row*120.0 + 120.0, 80.0, 120.0)];
[self.view addSubview:imageview];
[imageview release];
self.numPhotos = self.numPhotos + 1;
[self.navigationController dismissModalViewControllerAnimated:YES];
}
From running instruments I see that after I take the image, 8MB or so of space is taken up. But that space never gets released. So each time I take a photo, the memory goes up to hit 40MB which then causes the crash.
What am I doing wrong here? How do I clear the data taken by the UIImagePickerController class so that I'm not causing a crash after 5 pictures?
Maybe someone more familiar with iPhone development could give us a hint concerning this issue. I'm having the same memory issue with the image picker here.
//this is inside my ViewController.m
- (IBAction)openPhotolibrary:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//Now this seems to do the trick. The picker only gets allocated once. Therefore no memory issues any longer!
//Before I did it this way, my app crashed after picking 25 images. This one still was OK after picking about 50 images.
if (picker == nil) {
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
}
[self presentModalViewController:picker animated:YES];
}
}
- (void)useImage:(UIImage *)image {
//add code to use the picked image
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)CurrentPicker {
//hide the picker if user cancels picking an image.
[[CurrentPicker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[picker release];
[super dealloc];
}
Hope this helps.
Last edited by NewiPhoneDeveloper; 09-30-2008 at 04:17 AM.
imagepicker is deallocated in the dealloc function of my parent view controller.
Now, after a person takes a photo I do the following:
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
//we need to save each image and then show it in the interface!
int row = (self.numPhotos / 4);
int mod = self.numPhotos % 4;
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setFrame:CGRectMake(mod * 80.0, row*120.0 + 120.0, 80.0, 120.0)];
[self.view addSubview:imageview];
[imageview release];
self.numPhotos = self.numPhotos + 1;
[self.navigationController dismissModalViewControllerAnimated:YES];
}
From running instruments I see that after I take the image, 8MB or so of space is taken up. But that space never gets released. So each time I take a photo, the memory goes up to hit 40MB which then causes the crash.
What am I doing wrong here? How do I clear the data taken by the UIImagePickerController class so that I'm not causing a crash after 5 pictures?
Cheers
I must be missing something.
For every photo (1600x1200 at RGBA, so 8MB), you create a UIImageView which displays the image, of course the memory usage increases by 8MB. About 4 of those full size images will make iPhone run out of memory and crash.
The problem (at least in the above code) is not the image picker, it is your code.
This is still an issue. I've had the same problem. Per the recommendation of other users i created a singleton method on my app delegate to retain an instance of the pickercontroller if/when it's ever called and never release the object until the app shutsdown. I don't like this solution as it goes against everything we're supposed to avoid regarding memory management but i've not found any other method that helps. reasing the picturecontroller does nothing to reduce the memory.
I would love to find out what's going on here.. My app was denied by apple recently because they experienced crashes when picking pictures.. so i added the singleton and resubmitted....
John
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
I found out with my application, that it was not the picker controller. I learned that it was what I was doing with the images. Storing one UIImage that came from the camera took up a huge amount of memory. By the time I took 5, it crashed. I started to store my images as NSData instead. Whenever I needed to use the image, I would initialize it with that data. This way, you could release the picker controller and use it like you are suppose to. I found that it works so much better.
i agree that sounds like a good place to look. in my case i'm not letting users pick from the camera only the library. once a picture is chosen i'm compressing it down to a manageable size (< 200k) and i only ever show one image at a time. I'm still digging around but instruments is not showing me anything to go on at the moment.
I do have another screen that displays images.. i like the idea of using nsdata to store them.. will investigate that further.
Thanks!
John
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
I am using an UIImagePickerController to get a picture from the users library but I am having trouble doing exactly what i want to do; which is basically as follows:
Say I have a bounding box area specified on the screen (say 128 x 128 pixels). After the user has picked his photo and finalized zoom/scale etc and is done; I want only the area that is inside that bounding box to be retained: saved as it's own image inside the documents folder of the app for later user in the app. It is imperative that only the 128 x 128 final result is saved (for obvious memory reasons).
It's worth noting that the project I am working on is 2D OpenGL based (like GLSprite). Can anyone help me accomplish this?
scotopia i cant help with your question but you should throw it in a new thread as it's off-topic... we're dealing with memory issues at the moment!
John
__________________
---------------------------------------------------------------------- I love being a dad, flying airplanes and writing code.
----------------------------------------------------------------------
Follow me on Twitter: @BostonMerlin
Feed your brain on Twitter: @iPhoneDev101
----------------------------------------------------------------------
iPhone Apps:
It was in a new thread that got avoided like the plague; so I figured I'd pester you guys here cause I'm desperate. I didn't think it was really off topic though; because this does have to do with memory management of the UIImagePickerController resulting images...so its close. I thought it was determined that there was no "bug" and its just that the resulting images you end up are mammoth in memory size (8 megs) leading to a crash; so at it's heart, our issues are very related I think.
//this is inside my ViewController.m
- (IBAction)openPhotolibrary:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//Now this seems to do the trick. The picker only gets allocated once. Therefore no memory issues any longer!
//Before I did it this way, my app crashed after picking 25 images. This one still was OK after picking about 50 images.
if (picker == nil) {
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
}
[self presentModalViewController:picker animated:YES];
}
}
- (void)useImage:(UIImage *)image {
//add code to use the picked image
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)CurrentPicker {
//hide the picker if user cancels picking an image.
[[CurrentPicker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[picker release];
[super dealloc];
}
i agree that sounds like a good place to look. in my case i'm not letting users pick from the camera only the library. once a picture is chosen i'm compressing it down to a manageable size (< 200k) and i only ever show one image at a time. I'm still digging around but instruments is not showing me anything to go on at the moment.
I do have another screen that displays images.. i like the idea of using nsdata to store them.. will investigate that further.
Thanks!
John
I am also having the same problem. Have u rectified it. Then how to compress the captured image plz help me!!
I found out with my application, that it was not the picker controller. I learned that it was what I was doing with the images. Storing one UIImage that came from the camera took up a huge amount of memory. By the time I took 5, it crashed. I started to store my images as NSData instead. Whenever I needed to use the image, I would initialize it with that data. This way, you could release the picker controller and use it like you are suppose to. I found that it works so much better.
Hi, this is my first post in this page, its a great site and i've learned a lot. I have a question for you, i'm building an app where the user can select images i have uiimagepickercontroller and its all ok but how do you do to save those selected images to an image array to show on an imageview inside your app and that keeps that info even when you close and relaunch your app? Thanks in advance, happy coding
Edit: user picks images from photolibrary, not from camera
If you're displaying this view once for each image selection, then you will be creating a memory leak by allocating the image picker, but not releasing it.
The Apple sample releases the UIImagePickerController after an image has been selected. Your code doesn't do this.
This is the way to do it:
[picker release];
or
[picker autorelease];
From the code you posted, you're not doing this, so the previous allocs are sitting around in memory.
The reason your solution below alleviates the problem is that because you're conditionally allocating the picker, you only do it once.
Just thought I'd point this out to solve the mystery