Lev*_*rin 1 caching image ios afnetworking afnetworking-2
使用AFNetworking 2.0的方法setImageWithUrl,我在位于UITableViewCell的imageView中设置了一个图像.首次下载然后设置显示的图像时,它可以正常工作.但是,如果图像在设置时在本地可用(已缓存),则在显示之前会有快速白色闪烁.
你知道怎么避免这个吗?
重现步骤:
设置图像的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
myCell *cell = (myCell *)[tableView dequeueReusableCellWithIdentifier:@"imageCell"];
[cell.myImageView setImageWithURLRequest:[NSURLRequest requestWithURL:myImageUrl] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.myImageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Failed");
}];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
如果有人碰到同样的问题,我就是这样解决的:
在成功块中,替换
cell.myImageView.image = image;
Run Code Online (Sandbox Code Playgroud)
同
if (request) {
[UIView transitionWithView:cell.myImageView
duration:0.8f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{[cell.myImageView setImage:image];}
completion:NULL];
}else{
[cell.myImageView setImageWithURL:myImageURL];
}
Run Code Online (Sandbox Code Playgroud)
Voilà,不再是丑陋的闪光!
归功于这个答案,引领我走上正轨.