需要下载50,000张或更多图像

时间:2017-02-08 05:34:05

标签: ios objective-c

目前,我正在使用以下代码下载多张图片。在我的应用程序需要下载50,000或更多图像。 当内存消耗由于内存压力而达到600 MB应用程序崩溃时。

当我下载10K图像时工作正常。但是当我想从服务器下载20K图像时,下载9-10K图像应用程序因内存压力而崩溃。

我也尝试使用Instrument查找内存泄漏,但没有内存泄漏。

在调试会话中(下载20K图像时):
内存消耗:500-600 MB,然后崩溃 CPU使用率:130 -160%

你能帮我解决一下我在代码中做错了什么吗?

- (void)Downloadimages:(NSMutableArray *)aMutArray
{
    //newchange
    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    [imageCache clearMemory];
    [imageCache clearDisk];

    // NSLog(@"DocumentsDirectory Path : %@", DocumentsDirectory);
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];

    static AFURLSessionManager *sessionManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
    });

    __block int iCounter = 0;

    for (NSInteger aIndex = 0; aIndex < aMutArray.count; aIndex++)
    {

        NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:aMutArray[aIndex]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:(aMutArray.count * 120)];

        //   urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:aMutArray[aIndex]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:100];
        //NSLog(@"%@",urlRequest);
        // [urlRequest setTimeoutInterval:(aMutArray.count * 120)];


        NSURLSessionDownloadTask *downloadTask = [sessionManager downloadTaskWithRequest:req progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
            {
                return [NSURL fileURLWithPath:[DocumentsDirectory stringByAppendingPathComponent:urlRequest.URL.lastPathComponent]];
            }

            completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
            {
                iCounter ++;

                if (!error){
                    //  NSLog(@"(%ld) SUCCESS : %@",(long)aIndex, aMutArray[aIndex]);
                }
                else
                {
                    NSLog(@"(%ld) ERROR : %@",(long)aIndex, aMutArray[aIndex]);
                  //  [CommonMethod DeleteImageWithName:filePath.lastPathComponent];
                }

                [labelSyncProducts setText:[NSString stringWithFormat:@"Syncing Images %d of %lu", iCounter, (unsigned long)aMutArray.count]];
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];

                    if (aMutArray.count == iCounter)
                    {
                        [self controlsEnableDisable:true];
                    }
        }];

        [downloadTask resume];
    }
}

2 个答案:

答案 0 :(得分:1)

我建议您不要在应用中下载这么多图片,这会增加您在设备上的应用尺寸。

即使您想这样做,这也是解决方案。

如您所知,您下载的每个图像都会临时存储在RAM中。随着图像数量的增加,RAM占用率增加。在某一点上,RAM将满,应用终止。

要解决此问题,请下载一些图像并保存。保存后,从RAM中删除此图像并开始下载更多图像并保存。

继续此过程,直至完成所有图片。

这可以解决您的问题。

答案 1 :(得分:0)

问题不在于要下载的图像数量,下载任务不使用内存来保存图像,而只是将图像直接保存到沙盒中,这很好。但问题在于你运行的并发NSURLSession。想象一下,你在内存中有50,000个NSURLSession任务。这就是让应用程序崩溃的原因。

for (NSInteger aIndex = 0; aIndex < 50000; aIndex++)
{

  // NSURLSESSION BLOCK

}

您需要做的是排队下载。你需要自己处理这个问题。例如,一次首先下载10张图像,然后在10张图像完成后再下载10张,依此类推。但是......真的很疯狂。谁愿意等待下载50,000张图片?