从网址快速下载图片

时间:2016-06-23 08:14:15

标签: ios objective-c iphone

我正在使用Soap API,它将在XML文件中返回我存储在SQLite数据库中的xml文件的响应。该xml文件由图像url组成,我正在下载这些图像并将它们存储到本地文件夹中。但是下载和存储在本地文件夹中需要花费很多时间。现在我想要的是速度下载的逻辑并将它们存储在本地文件夹中。

这是我的代码:

 if (categoryArray.count > 0)
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"WordImages"];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:stringPath  withIntermediateDirectories:NO attributes:nil error:&error];
    NSString *fileName = [stringPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%lu.jpg",(unsigned long)categoryTitleArray.count]];
    NSString *imgUrl = [categoryArray objectAtIndex:0];
    imgUrl = [imgUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL  URLWithString:imgUrl]];
    [data writeToFile:fileName atomically:YES];
 categoryArray = [[NSMutableArray alloc]init];
    [imageArray addObject:fileName];

1 个答案:

答案 0 :(得分:2)

您可以使用sdwebimage库来下载图像并对其进行缓存。

Check my Answer here