0

i'm trying to create a photo viewer like the Apple Photos app in iOS. The layout is ok, but it receives memory warning and then crashes. Why? This happens even i load 7/8 images from the app documents folder. Have i to manage the memory with specific system? I use ARC with iOS 5.

EDIT :

The code :

for (int i=0; i<[dataSource count]; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[dataSource objectAtIndex:i] forState:UIControlStateNormal];
        [[button titleLabel] setText:[NSString stringWithFormat:@"%i",i+1]];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [[button layer] setBorderWidth:1];
        [[button layer] setBorderColor:[UIColor darkGrayColor].CGColor];
        if (i==0) {
            [button setFrame:CGRectMake(x, y, width, height)];
        } else {
            if (i%5==0) {
                nRow++;
                x=18;
                [button setFrame:CGRectMake(x, (y*nRow), width, height)];
            } else {
                [button setFrame:CGRectMake(x+space+width, (y*nRow), width, height)];
                x=button.frame.origin.x;
            }
        }
        [[self view] addSubview:button];
    }

The main part of this code is the first 6 lines, after is all x and y. dataSource is an NSArray declared as property (nonatomic, strong). It contains UIImage objects.

1
  • @user523234 simple Receive Memory Warning and then crashes Dec 21, 2011 at 10:44

2 Answers 2

1

You should be lazily loading your images in conjunction with reusing your buttons to account for the possibility of a large number of images.

To implement:

  1. Keep the paths to the image files in your data array instead of the UIImage objects. Get the image from the path using imageWithContentsOfFile: when you need it.
  2. Load the first z buttons into the scroll view where z is the number that appear on the screen at a time plus one row's worth.
  3. Set the UIViewController that we are currently in as the scrollview's delegate, and respond to changes in offset by repositioning buttons and setting appropriate images and targets.

Also, if 7/8 images is crashing your app, it sounds like you're dealing with some pretty large image files. Try to provide thumbnail-sized versions of the content within the documents directory(whatever the size of your buttons are EXACTLY), or if images are dynamic, see this post for a how-to.

0

If you are probably using ImageNamed, this article helped me alot:

http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/

Mainly

DO NOT USE [UIImage imageNamed] for any significant amount of images. It is EVIL. It WILL bring down your application and/or Springboard, even when your application is putting along using just barely a nibble of memory on its own.

and

it is better to implement your own cache

and here's the proposed cached image example:

- (UIImage*)thumbnailImage:(NSString*)fileName
{
   UIImage *thumbnail = [thumbnailCache objectForKey:fileName];

   if (nil == thumbnail)
   {
      NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName];
      thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
      [thumbnailCache setObject:thumbnail forKey:fileName];
   }
   return thumbnail;
}
1
  • @BartłomiejSemańczyk thanks for the prompt, I have updated my answer
    – 321zeno
    Feb 3, 2016 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.