Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2008, 08:26 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 9
Default 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?!
simsimma is offline   Reply With Quote
Old 09-14-2008, 09:01 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 7
Default

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.
hack4 is offline   Reply With Quote
Old 09-14-2008, 10:47 PM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 9
Default

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
simsimma is offline   Reply With Quote
Old 09-23-2008, 01:39 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 459
Default

Having the same issue here. Have you managed to solve that?
NewiPhoneDeveloper is offline   Reply With Quote
Old 09-23-2008, 11:04 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 52
Default

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.
johnnybluejeans is offline   Reply With Quote
Old 09-30-2008, 02:53 AM   #6 (permalink)
New Member
 
Join Date: Jul 2008
Location: Zurich, Switzerland
Age: 36
Posts: 15
Default

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.
mkroll is offline   Reply With Quote
Old 09-30-2008, 04:15 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 459
Default

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 04:17 AM.
NewiPhoneDeveloper is offline   Reply With Quote
Old 11-12-2008, 09:57 AM   #8 (permalink)
Airsource Ltd
 
Join Date: Nov 2008
Location: Cambridge, UK
Posts: 3
Default Memory issues in UIImagePickerController

I just wrote a blog post analysing the issue in a bit more depth at

The Airsource - Memory usage in UIImagePickerController

Airsource - Mobile Software Experts
The Airsource - A blog on mobile software
airsource is offline   Reply With Quote
Old 11-12-2008, 12:16 PM   #9 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 802
Default

So have you filed a bug report with all of this information to Apple?
scottiphone is offline   Reply With Quote
Old 11-12-2008, 01:21 PM   #10 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 812
Default

Quote:
Originally Posted by simsimma View Post
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.
johnqh is offline   Reply With Quote
Old 11-18-2008, 07:04 PM   #11 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 1
Default

Which Apple sample code are you using. Can you provide a link?

Thank you...
qjkbanks is offline   Reply With Quote
Old 01-01-2009, 06:30 AM   #12 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 5
Default

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?
vargonian is offline   Reply With Quote
Old 01-21-2009, 11:09 AM   #13 (permalink)
Lost in a sea of code
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 399
Default

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:
BostonMerlin is offline   Reply With Quote
Old 01-21-2009, 11:13 AM   #14 (permalink)
Registered Member
 
tkilmer's Avatar
 
Join Date: May 2008
Posts: 583
Default

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.
tkilmer is offline   Reply With Quote
Old 01-21-2009, 11:17 AM   #15 (permalink)
Lost in a sea of code
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 399
Default

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:
BostonMerlin is offline   Reply With Quote
Old 01-21-2009, 11:20 AM   #16 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,028
Default

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 05:33 PM.
scotopia is offline   Reply With Quote
Old 01-21-2009, 05:33 PM   #17 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,028
Default

::Bump::
scotopia is offline   Reply With Quote
Old 01-21-2009, 05:37 PM   #18 (permalink)
Lost in a sea of code
 
BostonMerlin's Avatar
 
Join Date: Apr 2008
Location: Boston
Posts: 399
Default

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:
BostonMerlin is offline   Reply With Quote
Old 01-21-2009, 05:48 PM   #19 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,028
Default

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.
scotopia is offline   Reply With Quote
Old 04-06-2009, 09:28 AM   #20 (permalink)
Tutorial Author
 
Join Date: Jun 2008
Location: Lahore, Pakistan
Posts: 34
Default

Quote:
Originally Posted by NewiPhoneDeveloper View Post
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!
adeem is offline   Reply With Quote
Old 06-16-2009, 07:54 AM   #21 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Madurai
Posts: 111
Send a message via MSN to lokidil Send a message via Yahoo to lokidil Send a message via Skype™ to lokidil
Default

Quote:
Originally Posted by BostonMerlin View Post
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!!
__________________
-----
http://codecruncher.wordpress.com
lokidil is offline   Reply With Quote
Old 06-20-2009, 12:02 AM   #22 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Madurai
Posts: 111
Send a message via MSN to lokidil Send a message via Yahoo to lokidil Send a message via Skype™ to lokidil
Default

Quote:
Originally Posted by simsimma View Post
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];
}
lokidil is offline   Reply With Quote
Old 05-03-2010, 03:01 AM   #23 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 2
Default Problem with use of Image pciking with UIImagePickerControl

Hello Can anyone help me ?

I am picking an image with a UIImagePickerControl with the code

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

if((UIButton *) sender == choosePhoto) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;


}
else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

}


I need to pas this image data to next view

i got the code for taking image data representation

UIImage *img = [UIImage imageNamed:@"some.png"];

NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);


how can I get the image representation of same image that i am taking with UIImagePickerControl

Can u pls send me the code to use that image ?
Bindu is offline   Reply With Quote
Old 05-26-2010, 12:11 AM   #24 (permalink)
New iPhone-iPad Developer
 
Join Date: May 2010
Posts: 24
Default

Quote:
Originally Posted by tkilmer View Post
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
JuanMazuera is offline   Reply With Quote
Old 01-09-2011, 05:11 AM   #25 (permalink)
Registered Member
 
spymaster's Avatar
 
Join Date: Jan 2011
Location: Melbourne, Australia
Posts: 1
Default Actually, you're not releasing the memory.

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
spymaster is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 443
16 members and 427 guests
Alexander_john, bev6a8hl, beyondstop, dapis, Edsilmars, HowEver, Hyde, john234ny, linkmx, MozyMac, pill5b3rry, pochuang, pufftissue, rianneadams, Rudy, samtakoy
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 157,848
Threads: 88,913
Posts: 379,292
Top Poster: BrianSlick (7,072)
Welcome to our newest member, rianneadams
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0