下载大量图像ipad - 内存泄漏? [英] downloading large amount of images ipad - memory leak?

查看:97
本文介绍了下载大量图像ipad - 内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要下载大量图像的应用程序,可能是10,000个。现在,在内存不足和应用程序崩溃之前,我可以达到大约3000,这取决于图像文件的大小。我正在后台线程下载它们并向用户显示进度。

I am have an app I am writing that will need to download alot of images, possibly 10,000. Right now I can get to about 3000 before I run out of memory and the app crashes, just depends on the image files sizes. I am downloading them on a background thread and showing the progress to the user.

我写了一个帮助类,我正在访问这样做,并想知道这是否是我的问题所在谎言,我只是泄漏记忆。

I wrote a helper class thatI am accessing to do this and wondering if that is where my problems lie and I am just leaking memory.

这是我的循环下载图像 - 这是在后台线程上运行,这都在NSAutoReleasePool中:

Here is my loop that downloading the images - this is running on a background thread, this is all inside an NSAutoReleasePool:

for (int j=0; j < [items count]; j++)
            {

                ImagesHelper *helper = [[ImagesHelper alloc]init];
                [helper downloadImages: [[items objectAtIndex:j] valueForKey:@"ItemSKU"] withManufacturer: [[manufacturers objectAtIndex:i] ManufacturerID]];
                [helper release];

                if (j%50==0) { //this notifies the user of progress
                    statusMessage = [NSString stringWithFormat:@"Downloading images: %@.jpg (%d of %d)", [[items objectAtIndex:j] valueForKey:@"ItemSKU"],j+1, [items count]];
                    [self performSelectorOnMainThread:@selector(setStatus) withObject:nil waitUntilDone:YES];
                }
            }

这是我的助手类:

-(void) downloadImages:(NSString *)ItemSKU withManufacturer: (NSString *) aManufacturerID{

    NSData *imageData = nil;
    NSData *imageDataLarge=nil;
    NSString *fileName = [NSString stringWithFormat:@"%@_tn.jpg", [ItemSKU  stringByReplacingOccurrencesOfString:@" " withString:@""]];
    NSString *fileNameLarge = [NSString stringWithFormat:@"%@_lg.jpg", [ItemSKU stringByReplacingOccurrencesOfString:@" " withString:@""]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *savePath = [documentsPath stringByAppendingPathComponent:fileName];
    NSString *savePathLarge = [documentsPath stringByAppendingPathComponent:fileNameLarge];

    /* go get the image */
    NSString *URL = [NSString stringWithFormat:kProductImagesURL,aManufacturerID, fileName];
    NSString *URLLarge = [NSString stringWithFormat:kProductImagesURL,aManufacturerID, fileNameLarge];

    NSLog(@"Going to get the file: %@",URL);
    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
    if (imageData!=nil) {
        [imageData writeToFile:savePath atomically:YES];
    }

    imageDataLarge = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLLarge]];
    if (imageDataLarge!=nil) {
        [imageDataLarge writeToFile:savePathLarge atomically:YES];
    }

    imageData = nil;
    imageDataLarge=nil;
}

仍然试图了解其中一些概念。

Still trying to get ahold of some of these concepts.

推荐答案

您是否尝试过构建和分析?这可能有所帮助。也可以玩乐器。最后,我建议使用NSOperationQueue。这样你就可以并行完成它们。此外,在控制权返回主线程之前,您的AutoReleasePool可能不会自动释放任何内容。

Have you tried doing Build and Analyze? That might help. Also play around with Instruments. Lastly, I'd recommend using NSOperationQueue for these. That way you can do them in parallel. Additionally, your AutoReleasePool might not autorelease anything until control returns to the main thread.

这篇关于下载大量图像ipad - 内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆