Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 分配多个对象而不发生崩溃的最佳方法是什么?_Iphone_Objective C_Ios4_Allocation - Fatal编程技术网

Iphone 分配多个对象而不发生崩溃的最佳方法是什么?

Iphone 分配多个对象而不发生崩溃的最佳方法是什么?,iphone,objective-c,ios4,allocation,Iphone,Objective C,Ios4,Allocation,我正在编写一个应用程序,它从服务器上获取图像,并将所有图像作为UIButton显示给用户,这样我就可以截获服务器上的事件。问题是,分配所有图像需要很长时间,当它最终显示所有图像时,应用程序在实际设备上崩溃(在模拟器上工作) 在没有崩溃的情况下,分配这些图像对象的正确方法是什么 提前谢谢 这是我目前的密码 for(start = 1; start <= limit; start++) { NSString *tempstring; if(start < 10)

我正在编写一个应用程序,它从服务器上获取图像,并将所有图像作为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;

    }

for(start=1;start很可能图像的大小大于您试图放入图像的大小,但是当您一起读取它们时,它们会占用大量内存

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


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

Kendall Helmstetter Geln的答案可能会给你带来最好的单一改进,尽管如果没有看到更多的程序以及它如何执行,很难确定

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

  • 发布不可见的图像(至少部分图像)

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

  • 尽可能重用/共享(内存中)映像

  • 更喜欢创建不自动删除的对象。例如,使用
    alloc
    +
    init
    +(稍后)
    release

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

  • 配置文件,以确定您的分配真正增长的原因/位置

  • 如果您正忙于分配大量资源的操作队列,也许您应该将其设置为串行:
    [NSOperationQueue setMaxConcurrentOperationCount:1]


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

正确!图像的大小从1680px调整到70px。显然,分配180多个对象时会占用大量内存。谢谢!我现在只将操作队列更改为一个。谢谢!我甚至没有看closley的原始解决方案,但你是对的,某种解决方案隐藏不可见的图像确实是一个好主意。特别是这里正在做的事情似乎真的需要使用一个带有可重复使用单元格的表格,在滚动时加载图像。一个有趣的项目是AQGridView,它可以帮助以更节省内存的方式显示拇指。