Array of Images Causing Memory Problems

Discussion in 'iOS Programming' started by CNuland, Jun 29, 2010.

  1. CNuland macrumors newbie

    Jun 17, 2010
    In my app I have an array of objects, called annotatedImages. One of the fields of this object is UIImage *image. I also have an imagePickerController using the camera. When the user takes an image it then creates a new annotatedImage and sets the image of that object to the cameras picture. This works fine up till about the forth picture, when I get a "Received memory warning. Level=2" which then results in the program crashing and I receive this error
    I'm guessing that the iphones memory is being overloaded somehow. Do you have any suggestions on how to fix this? Here's the code where the object is being added to the array in the imagePickerController. I should also mention that I tried releasing the image, but I got a EXC_BAD_ACCESS error, which I'm guessing is due to the newly created objects image pointing to that image.
    - (void) imagePickerController: (UIImagePickerController *)picker
     didFinishPickingMediaWithInfo:(NSDictionary *)info {
    	UIImage *image;
    	NSURL *mediaUrl;
    	mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
    	if(mediaUrl == nil){
    		image = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
    		if(image == nil){
    			image = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
    			[image retain];
    			imageView.image = image;
    			AnnotatedImage *a = [[AnnotatedImage alloc] initWithName:@"Picture" description:@"mmmm"];
    			[a setImage: image];
    			[images addObject:a];			
    			imageView.image = image;
    	else {
    	[picker dismissModalViewControllerAnimated:YES];

    edit: It seems the crash is actually happening when I try to load this array into a table. Would it be better to use another format to store the image? like NSData? Here's my code for the table.

    - (UITableViewCell *)tableView:(UITableView *)tableView 
    		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	//create cell
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"test"];
    	if(cell == nil) {
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"] autorelease];
    	a = [globalMainVC.images objectAtIndex:indexPath.row];
    	//fill cell with content
    	cell.textLabel.text =;
    	cell.imageView.image = a.image;
    	return cell; 
  2. CNuland thread starter macrumors newbie

    Jun 17, 2010
    Alright, so I removed the line where the cells imageview is being set to the objects image and the app runs fine with no warnings or crashes. I'm guessing I need to thumbnail the image or reduce the size somehow... any suggestions?
  3. CNuland thread starter macrumors newbie

    Jun 17, 2010
    Is This a Memory Leak?

    I posted a few days ago about my program crashing after a memory warning. I figured out it was due to a table loading full UIImages for the imageview. I fixed this by making thumbnail versions of the pictures. However, my program will now crash around the 13th picture taken.

    My app saves pictures the user has taken in an array and then uploads them to a webserver. Right now if the array holds about 13+ pictures I get the "Received memory warning. Level=1", eventually it crashes around the 15th or so picture. I'm wondering if this just means I've reached the maximum amount of pictures in that array that the memory will allow for, or if this is a memory leak of some sort. If it is the prior, would there be a better way to approach this? I'm thinking of saving the image data to the phone, then making an array using the file location reference, but I fear that would add a great deal of time between each picture taken.

    Thanks for any help you can give.


Share This Page