 |
 |
|
 |
09-14-2008, 07:26 PM
|
#1 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 9
Rep Power: 0
|
UIImagePickerController memory issues!
Hi,
So in my application I want the user to be able to take as many photos as they like and store them for future lookup.
I use the sample code online for instantiating the camera.
Once the image is grabbed I use UIImageView to show the image to the user.
The problem is that after taking five pictures or so the application crashes!
Has anyone else had any issues with the camera and what did people have to do to fix it?!
|
|
|
09-14-2008, 08:01 PM
|
#2 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 6
Rep Power: 0
|
Are you sure you're releasing the image picker once you've used it?
A lot of the sample code you see online doesn't seem to do that.
|
|
|
09-14-2008, 09:47 PM
|
#3 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 9
Rep Power: 0
|
I think so, here is the code:
Code:
-(BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
{
if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
|| (delegateObject == nil) || (controller == nil))
return NO;
if(imagepicker == nil){
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagepicker.delegate = delegateObject;
}
//picker.allowsImageEditing = YES;
// Picker is displayed asynchronously.
[controller presentModalViewController:imagepicker animated:YES];
return YES;
}
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
|
|
|
09-23-2008, 12:39 AM
|
#4 (permalink)
|
|
Senior Member
Join Date: Jul 2008
Posts: 246
Rep Power: 1
|
Having the same issue here. Have you managed to solve that?
|
|
|
09-23-2008, 10:04 AM
|
#5 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 6
Rep Power: 0
|
I too have this issue. If you follow Apple's sample code exactly, you will get a crash after about 5-6 photos because of memory use.
|
|
|
09-30-2008, 01:53 AM
|
#6 (permalink)
|
|
Junior Member
Join Date: Jul 2008
Location: Zurich, Switzerland
Age: 34
Posts: 15
Rep Power: 0
|
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.
Thanks,
Michael.
|
|
|
09-30-2008, 03:15 AM
|
#7 (permalink)
|
|
Senior Member
Join Date: Jul 2008
Posts: 246
Rep Power: 1
|
Hi there,
let me share the piece of code with you, that finally solved my memory issues!
Code:
//this is my ViewController.h
@interface myViewController: UIViewController <UIImagePickerControllerDelegate> {
UIImagePickerController *picker;
}
@property (nonatomic, retain) UIImagePickerController *picker;
- (IBAction)openPhotolibrary:(id)sender;
- (void)useImage:(UIImage *)image;
@end
Code:
//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 03:17 AM.
|
|
|
11-12-2008, 11:16 AM
|
#9 (permalink)
|
|
Senior Member
Join Date: Apr 2008
Posts: 802
Rep Power: 2
|
So have you filed a bug report with all of this information to Apple?
|
|
|
11-12-2008, 12:21 PM
|
#10 (permalink)
|
|
Senior Member
Join Date: Nov 2008
Posts: 737
Rep Power: 1
|
Quote:
Originally Posted by simsimma
I think so, here is the code:
Code:
-(BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
{
if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
|| (delegateObject == nil) || (controller == nil))
return NO;
if(imagepicker == nil){
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagepicker.delegate = delegateObject;
}
//picker.allowsImageEditing = YES;
// Picker is displayed asynchronously.
[controller presentModalViewController:imagepicker animated:YES];
return YES;
}
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.
|
|
|
11-18-2008, 06:04 PM
|
#11 (permalink)
|
|
Junior Member
Join Date: Nov 2008
Posts: 1
Rep Power: 0
|
Which Apple sample code are you using. Can you provide a link?
Thank you...
|
|
|
01-01-2009, 05:30 AM
|
#12 (permalink)
|
|
New Member
Join Date: Oct 2008
Posts: 4
Rep Power: 0
|
I'm seeing this leak too, on the device. I'm pulling my hair out trying to figure out if it's my code or something with the UIImagePickerController.
Can anyone confirm that this is still an issue?
|
|
|
01-21-2009, 10:09 AM
|
#13 (permalink)
|
|
Lost in a sea of code
iPhone Dev SDK Supporter
Join Date: Apr 2008
Location: Boston
Posts: 367
Rep Power: 0
|
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
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
|
|
|
01-21-2009, 10:13 AM
|
#14 (permalink)
|
|
Senior Member
Join Date: May 2008
Posts: 575
Rep Power: 0
|
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.
|
|
|
01-21-2009, 10:17 AM
|
#15 (permalink)
|
|
Lost in a sea of code
iPhone Dev SDK Supporter
Join Date: Apr 2008
Location: Boston
Posts: 367
Rep Power: 0
|
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
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
|
|
|
01-21-2009, 10:20 AM
|
#16 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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?
Last edited by scotopia; 01-21-2009 at 04:33 PM.
|
|
|
01-21-2009, 04:33 PM
|
#17 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
::Bump::
|
|
|
01-21-2009, 04:37 PM
|
#18 (permalink)
|
|
Lost in a sea of code
iPhone Dev SDK Supporter
Join Date: Apr 2008
Location: Boston
Posts: 367
Rep Power: 0
|
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
----------------------------------------------------------------------
iPhone Apps: The Pilots Library
|
|
|
01-21-2009, 04:48 PM
|
#19 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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.
|
|
|
04-06-2009, 08:28 AM
|
#20 (permalink)
|
|
Tutorial Author
Join Date: Jun 2008
Location: Lahore, Pakistan
Posts: 29
Rep Power: 0
|
Quote:
Originally Posted by NewiPhoneDeveloper
Hi there,
let me share the piece of code with you, that finally solved my memory issues!
Code:
//this is my ViewController.h
@interface myViewController: UIViewController <UIImagePickerControllerDelegate> {
UIImagePickerController *picker;
}
@property (nonatomic, retain) UIImagePickerController *picker;
- (IBAction)openPhotolibrary:(id)sender;
- (void)useImage:(UIImage *)image;
@end
Code:
//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.
|
Thanks  it work for me!
|
|
|
06-16-2009, 06:54 AM
|
#21 (permalink)
|
|
Member
Join Date: Apr 2009
Posts: 73
Rep Power: 1
|
Quote:
Originally Posted by BostonMerlin
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!!
|
|
|
06-19-2009, 11:02 PM
|
#22 (permalink)
|
|
Member
Join Date: Apr 2009
Posts: 73
Rep Power: 1
|
Quote:
Originally Posted by simsimma
Hi,
So in my application I want the user to be able to take as many photos as they like and store them for future lookup.
I use the sample code online for instantiating the camera.
Once the image is grabbed I use UIImageView to show the image to the user.
The problem is that after taking five pictures or so the application crashes!
Has anyone else had any issues with the camera and what did people have to do to fix it?!
|
Hey i m with solution now check out this code this will allow us to take infinite photos
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
unsigned char* rawData;
CGRect thumbRect = CGRectMake(0,0,480,480);
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
//4 * thumbRect.size.width, // rowbytes
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
alphaInfo
);
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -thumbRect.size.width, 0);
CGContextDrawImage(bitmap, thumbRect, imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
image = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
thumbRect = CGRectMake(0,0,320,430);
bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
//4 * thumbRect.size.width, // rowbytes
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
alphaInfo
);
CGContextDrawImage(bitmap, thumbRect, ref);
CGImageRelease (ref);///////////new
ref = CGBitmapContextCreateImage(bitmap);
image = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if(rawData)free(rawData);////////////////new
rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef contextC = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(contextC, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(contextC);
imgView.image =[[[UIImage alloc] initWithData:UIImageJPEGRepresentation(image, 1.0)] autorelease];//Where imgView is the UIImageView
[picker dismissModalViewControllerAnimated: YES];
[picker release];
}
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 182 |
| 11 members and 171 guests |
| Alexman, atsd, BostonMerlin, comtek, lepetitapps, Neverever, P-atr1k, Raphy, Slecorne, Stitch, _mubashir |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,172
Threads: 20,132
Posts: 89,979
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, juicysen |
|