tableView调用reloaddata的时候发现有个小问题,每次刷新图片都会抖动闪烁一下,看着很难受,也影响体验。造成这个问题的主要原因是因为刷新时候切换图片导致。要解决这个问题也很好解决,使用SDWebImage会缓存已下载图片的特性,当要加载的图片图片的时候,先从缓存中去读,如果能读到直接把读到的图片作为新的placehodler,无法读到则使用默认placeholder.
代码如下:
UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url.absoluteString];
[self.imgView sd_setImageWithURL:url placeholderImage:cacheImage?cacheImage:HXQDefaultPlaceholderImage];
import "UIImageView+YYWebImage.h"
NSString *strurl =[model.adImgUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlImage =[NSURL URLWithString:strurl];
YYWebImageManager *manager = [YYWebImageManager sharedManager];
NSString *cacheKey = [manager cacheKeyForURL:urlImage];
NSData *data = (id)[manager.cache.diskCache objectForKey:cacheKey];
UIImage *imageHuancun = [UIImage imageWithData:data];
// 最后一行或者用下面,两个效果一样
// NSData *data = (id)[manager.cache getImageDataForKey:cacheKey];
//改成YYImage加载
// [self.image setImageWithURL:[NSURL URLWithString:strurl] placeholder:[StringUtil getImageByResName:@"XCHomeBannerdefault.png"]];
// [self.image setImageWithURL:urlImage placeholder:imageHuancun?imageHuancun:[StringUtil getImageByResName:@"XCHomeBannerdefault.jpg"]];
[self.image setImageWithURL:urlImage placeholder:imageHuancun];
网友评论