开发者

What is the best way to allocate multiple objects without a crash?

I'm writing an app that takes images from a server and displays all the images to the user as a UIButton so that I can intercept events on it. The problem is, it takes a really long time to allocate all the images, and when it finally displays all the images, the app crashes on the actual device (works on the simulator).

What would be the proper way of allocating these image objects without a crash?

Thanks in advance!

Here's my current code

 for(start = 1; start <= limit; start++) {

     NSString *tempstring;

     if(start < 10) {
     tempstring = [NSString stringWithFormat:@"0%d", start];
     } else {
     tempstring = [NSString stringWithFormat:@"%d", start];
     }

     NSOperationQueue *queue = [NSOperationQueue new];
     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(displayThumbs:) object:[NSString stringWithFormat:@"%d", start]];

     [queue addOperation:operation];
     [operation release];
     [queue release];

 }


- (void)displayThumbs:(NSString *)buttoninfo {

        int currentmag = [buttoninfo intValue];
        currentmag--;
        currentmag = (currentmag * 70) + 10;

        NSString *tempstring;
        if([buttoninfo intValue] < 10) {
            tempstring = [NSString stringWithFormat:@"0%@", buttoninfo];
        } else {
            tempstring = [NSString stringWithFormat:@"%@", buttoninfo];
        }   

        UIButton *magbutton = [[UIButton alloc] initWithFrame:CGRectMake(currentmag, 10, 50, 50)];
        magbutton.tag = [buttoninfo intValue];
        开发者_如何学Python[magbutton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_Page_%@.jpg", @"2011-02", tempstring]] forState:UIControlStateNormal];
        [magbutton addTarget:self action:@selector(gotoStory:) forControlEvents:UIControlEventTouchUpInside];
        [thumb_scoller addSubview:magbutton];

        [magbutton release];
        magbutton = nil;

    }


Very probably the images are larger than the size you are trying to put them in, but when you are reading them all together they take up a lot of memory.

At the time you download the images you should save off thumbnail versions that are smaller you could use for the buttons.

It's crashing on the device because that has a memory limit for processes where the simulator does not.


Kendall Helmstetter Geln's answer is likely going to give you the best singular improvement, although it's hard to be certain without seeing more of the program, and how it executes.

In addition to resizing the images to the displayed size, some other tips:

  • release images which are not visible (at least, some of them)

  • use a local on-disk cache for your images (including images which are resized)

  • reuse/share (in-memory) images where possible

  • prefer creating objects which are not autoreleased. e.g., use alloc+init+(later)release

  • explicitly create/destroy autorelease pools around blocks which create many autoreleased objects and/or large autoreleased allocations

  • profile to determine why/where your allocations really grow.

  • if you're farming off to an operation queue which allocates a lot, perhaps you should make it serial: [NSOperationQueue setMaxConcurrentOperationCount:1]

curiosity: why create an operation queue per iteration?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜