AFImageRequestOperation与SDWebImage问题

时间:2013-04-02 06:46:25

标签: iphone ios afnetworking sdwebimage

我使用AFImageRequestOperation下载一些图标,同时使用SDWebImage下载主视图的一些图片。每个AFImageRequestOperation都添加到app delegate中定义的publicOperationQueue中,其maxConcurrentOperationCount设置为10.奇怪的是,有时我的10个图标中的一个或两个将被主视图中的某些图片替换,应由SDWebImage下载。当我设置一个比我的图标计数更大的maxConcurrentOperationCount时,它工作正常。我怀疑它是否与多个NSOperationQueues共享一些资源和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方法下载pic

1 个答案:

答案 0 :(得分:0)

好的,从问题发现你正在使用AFNetworking。然后用于图像下载为什么不使用AFNetworking的UIImageView Extention? 你不必为此实现任何队列或任何东西 像这样的一些代码就足够了我认为

  [self.imageview setImageWithURL:url placeholderImage:nil];