简体   繁体   English

内存压力下载许多图像

[英]Memory pressure downloading many images

I am trying to download thousands of pictures (Maximum 350 Kb each) from the server, but I go by over a thousand images I receive the alert "Memory Presure". 我正在尝试从服务器下载数千张图片(每张最大350 Kb),但是我浏览了超过一千张图片,并收到警报“ Memory Presure”。

Basically I have an array with all the names of the images and do a loop to bring one to one like this: 基本上,我有一个包含所有图像名称的数组,并执行循环以使图像像这样一对一:

for (int x=0; x<unique.count; x++) {

     NSURL *ImageLink = [NSURL URLWithString:[NSString stringWithFormat:@"http://urltoimagesfolder.com/", [unique objectAtIndex:x]]];
     NSData *data = [NSData dataWithContentsOfURL:ImageLink];
     UIImage *img = [[UIImage alloc] initWithData:data];

     if (data.length !=0) {

     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[unique objectAtIndex:x]]; //add our image to the path

     [UIImageJPEGRepresentation(img, 1.0) writeToFile:fullPath atomically:YES];

     //[self saveImage:img :NombreFoto];
     //[self Miniatura:img :[NSString stringWithFormat:@"mini-%@", [unique objectAtIndex:x]]];
     }

    data = nil;
    img = nil;


}

Question: How I can download all the images without the app crash with memory pressure? 问题:如何在应用程序不因内存不足而崩溃的情况下下载所有图像?

UIImageJPEGRepresentation() may cause the memory overflow. UIImageJPEGRepresentation()可能会导致内存溢出。 But you don't need to use that function, you can check if the received data is image and write its bytes directly to disk via sending message writeToFile: to data object. 但是您不需要使用该功能,可以检查接收到的数据是否为图像,并通过向data对象发送消息writeToFile:将其字节直接写入磁盘。

You can fix you code like this: 您可以这样修改代码:

for (int x=0; x<unique.count; x++) {
     NSURL *ImageLink = [NSURL URLWithString:[NSString stringWithFormat:@"http://urltoimagesfolder.com/", [unique objectAtIndex:x]]];
     NSData *data = [NSData dataWithContentsOfURL:ImageLink];
     if (data.length !=0) {

         UIImage *img = [[UIImage alloc] initWithData:data];
         if (img) {
             NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[unique objectAtIndex:x]]; //add our image to the path
             [data writeToFile:fullPath atomically:YES];
         }
         img = nil;

     }
     data = nil;
}

However this is not the optimal solution. 但是,这不是最佳解决方案。 -dataWithContentsOfURL: is synchronous method and will stop execution of your main thread while downloading the file. -dataWithContentsOfURL:是同步方法,在下载文件时将停止执行主线程。 As a consequence the UI will hang during the download. 结果,UI将在下载过程中挂起。 To make your UI not to hang you can use asynchronous url requests. 为了使UI不会挂起,可以使用异步url请求。

See -sendAsynchronousRequest:queue:completionHandler: method of the NSURLConnection class. 请参阅-sendAsynchronousRequest:queue:completionHandler: NSURLConnection类的方法。 Or if your app is only for iOS 7 see -dataTaskWithURL:completionHandler: as alternative. 或者,如果您的应用程序仅适用于iOS 7,请参见-dataTaskWithURL:completionHandler:作为替代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM