【发布时间】:2016-12-07 05:37:27
【问题描述】:
在我的应用程序中,我需要下载大约 6,000 图像。我知道这很多,但这是必需的。
目前我正在执行以下操作:
NSArray *photos = @[hugeAmountOfPhotoObjects];
for (ZSSPhoto *photo in photos) {
[self downloadImageWithURL:photo.mobileURL progress:^(double progress) {
} completion:^(UIImage *image) {
// Save the image
} failure:^(NSError *error) {
}];
}
...
- (void)downloadImageWithURL:(NSURL *)url progress:(void (^)(double progress))progress completion:(void (^)(UIImage *image))completion failure:(void (^)(NSError *error))failure {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:600];
self.operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self processOperation:operation error:error failure:failure];
}];
[requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
progress(percentDone);
}];
[self.operationQueue addOperation:requestOperation];
}
这里的问题是使用这种方法下载需要很长时间,并且我的一些用户报告由于内存使用率高而崩溃。
有没有更好的方法可以用来下载如此大量的图像文件?
【问题讨论】:
-
您是否要下载所有照片,然后将其放入数组中,然后仅将其保存到磁盘?
-
@Tj3n - 不,当完成块下载并返回每个图像时,我会将它们保存到磁盘。
-
我会设置一些机制,一次只下载几个,可能有一个未下载的图像数组,然后是一个 for 循环,将其中的 3 个取出,下载它们,然后在完成提取时还有 3 个等
-
我希望您不要通过用户的蜂窝连接进行所有这些下载。
-
@rmaddy - 这是用户发起/导入的操作。他们正在导入自己的带有图像链接的文件。
标签: ios objective-c afnetworking-2