我正在使用UIImageView+AFNetworking
从表视图上的远程服务器加载图像。但是,在某些情况下,错误的图像被缓存(在NSURLCache
上,而不仅仅是AFImageCache
),即来自URL的缓存返回来自另一个的响应(均在应用程序上使用)。
我的tableView:cellForRowAtIndexPath:
非常正常:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dict = self.sections[indexPath.section][indexPath.row];
static NSString *CellIdentifier = @"Cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImage *placeholder = [[UIImage imageNamed:@"shield-flat"] tintedImageWithColor:[UIColor asbestosColor]];
if (dict[@"first_url"] != [NSNull null]) {
NSURLRequest *mandanteReq = [NSMutableURLRequest imageRequestWithURL:[NSURL URLWithString:dict[@"first_url"]]];
[cell.firstImageView setImageWithURLRequest:mandanteReq placeholderImage:placeholder success:NULL failure:NULL];
} else {
cell.firstImageView.image = placeholder;
}
if (dict[@"second_url"] != [NSNull null]) {
NSURLRequest *visitanteReq = [NSMutableURLRequest imageRequestWithURL:[NSURL URLWithString:dict[@"second_url"]]];
[cell.secondImageView setImageWithURLRequest:visitanteReq placeholderImage:placeholder success:NULL failure:NULL];
} else {
cell.secondImageView.image = placeholder;
}
return cell;
}
此外,我在NSMutableURLRequest
上的类别用于创建请求:
+ (NSMutableURLRequest *)imageRequestWithURL:(NSURL *)url {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
request.HTTPShouldHandleCookies = NO;
request.HTTPShouldUsePipelining = YES;
request.timeoutInterval = 10;
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
return request;
}
我知道dict
是当前对象,而且网址也很好。我也相信服务器(Amazon S3)正在发送正确的图像。
问题经常发生在应用程序的第一次安装上(可能是因为之后缓存了图像)。
有什么想法?