我使用 AFImageRequestOperation 下载一些图标,同时使用 SDWebImage 下载一些图片用于主视图。每个 AFImageRequestOperation 都添加到我在 app 委托中定义的 publicOperationQueue 中,它的 maxConcurrentOperationCount 设置为 10。奇怪的是,有时我的 10 多个图标中的一个或两个会被主视图中的一些图片替换,这些图片应该由 SDWebImage 下载。当我设置一个大于我的图标计数的 maxConcurrentOperationCount 时,它工作正常。我怀疑它是否与多个 NSOperationQueue 共享一些资源和 maxConcurrentOperationCount 有关。任何人都可以帮忙吗?
//below is the icon downloading code
//============================//
for(NSString *url in picUrls)
{
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[urlRequest setHTTPShouldHandleCookies:NO];
[urlRequest addValue:@"image/*" forHTTPHeaderField:@"Accept"];
AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSString *imageName = trimNilOrNuLL([url lastPathComponent]);
if(imageName.length > 0)
{
NSData *imageData = UIImagePNGRepresentation(responseObject);
[imageData writeToFile:[path stringByAppendingPathComponent:imageName] atomically:YES];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"%@",error);
}];
[[AppShare appDelegate].publicOperationQueue addOperation:requestOperation];
}
//============================//
对于 SDWebImage,我在 UIImageView+WebCache 类别中使用 - (void)setImageWithURL:(NSURL *)url 方法来下载图片