Ios 下载许多图像时的内存压力

Ios 下载许多图像时的内存压力,ios,xcode,image,memory,download,Ios,Xcode,Image,Memory,Download,我试图从服务器上下载数千张图片(每张最大350 Kb),但我浏览了超过1000张图片,我收到了“内存压力”警报 基本上,我有一个包含所有图像名称的数组,并执行一个循环,使一对一像这样: for (int x=0; x<unique.count; x++) { NSURL *ImageLink = [NSURL URLWithString:[NSString stringWithFormat:@"http://urltoimagesfolder.com/", [unique ob

我试图从服务器上下载数千张图片(每张最大350 Kb),但我浏览了超过1000张图片,我收到了“内存压力”警报

基本上,我有一个包含所有图像名称的数组,并执行一个循环,使一对一像这样:

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;


}
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;
}

for(int x=0;x
UIImageJPEGRepresentation()
可能导致内存溢出。 但您不需要使用该功能,您可以检查接收到的数据是否为图像,并通过向
对象发送消息
writeToFile:
将其字节直接写入磁盘

您可以像这样修复代码:

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;


}
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;
}

for(int x=0;x你的问题是什么?我如何下载所有的图像而不让应用程序在内存压力下崩溃?嗯…在你写下img后释放它?