在不崩溃的情况下分配多个对象的最佳方法是什么?

发布于 2024-10-17 09:24:35 字数 1702 浏览 2 评论 0原文

我正在编写一个应用程序,它从服务器获取图像并将所有图像作为 UIButton 显示给用户,以便我可以拦截其上的事件。问题是,分配所有图像需要很长时间,当它最终显示所有图像时,应用程序在实际设备上崩溃(在模拟器上工作)。

在不崩溃的情况下分配这些图像对象的正确方法是什么?

提前致谢!

这是我当前的代码

 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];
        [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;

    }

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];
        [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;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2024-10-24 09:24:35

这些图像很可能比您尝试放入的尺寸大,但是当您一起阅读它们时,它们会占用大量内存。

下载图像时,您应该保存可用于按钮的较小缩略图版本。

它在设备上崩溃,因为它对进程有内存限制,而模拟器没有。

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.

画尸师 2024-10-24 09:24:35

Kendall Helmstetter Geln 的答案可能会给您带来最好的单一改进,尽管在没有看到更多程序及其执行方式的情况下很难确定。

除了将图像调整为显示大小之外,还有一些其他提示:

  • 释放不可见的图像(至少其中一些)

  • 为您的图像(包括调整大小的图像)使用本地磁盘缓存

  • 重用/共享(in-尽可能

  • 更喜欢创建不自动释放的对象。例如,使用 alloc+init+(later)release

  • 在创建许多自动释放对象的块周围显式创建/销毁自动释放池和/或大型自动释放分配

  • 分析以确定分配真正增长的原因/位置。

  • 如果你要分配给一个分配很多的操作队列,也许你应该将其串行化:[NSOperationQueue setMaxConcurrentOperationCount:1]

好奇心:为什么每次迭代都创建一个操作队列?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文