经过测试发现,内存飙升的最大原因是使用了SDWebImageRefreshCached,不使用就行了;(不是很懂,但是考虑到UIImage imageName不会释放内存,应该是同样的原因)
//采用动态替换加载方式,在视界内的图片先加载缩略图,后加载
////异步的方式来获取硬盘缓存的图片 diskImageExistsWithKey 判断不准确
[[SDImageCache sharedImageCache] queryCacheOperationForKey:imagePath done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
if(image)
{
NSLog(@"图片disk中存在原图,加载本地图片%@",imagePath);
dispatch_async(dispatch_get_main_queue(), ^{
__block UIActivityIndicatorView *activityIndicator;
__block UIProgressView *pv;
//对于少量的分辨率图片会有效果。但是对当前所要解决的大量(一屏显示30-40张)的高质量图片,依旧会因为内存激增而崩溃。
UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imagePath];
// 这里的options一定不能设置//SDWebImageRefreshCached ,会导致内存不断飙升
SDWebImageOptions newOptions = SDWebImageRetryFailed | SDWebImageHighPriority | SDWebImageProgressiveLoad | SDWebImageAvoidDecodeImage | SDWebImageScaleDownLargeImages;
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:cacheImage?cacheImage:self.placeholderImage options:newOptions context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
dispatch_main_sync_safe(^{
// your code
float currentProgress = (float)receivedSize/(float)expectedSize;
if (!activityIndicator)
{
[cell.imageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = cell.imageView.center;
}
[activityIndicator startAnimating];
if(currentProgress >= 1)
{
if(activityIndicator)
{
// [activityIndicator removeFromSuperview];
[ activityIndicator stopAnimating ];//停止
}
}
// if(!pv)
// {
// pv = [[UIProgressView alloc]initWithProgressViewStyle:(UIProgressViewStyleDefault)];
// pv.frame = CGRectMake(0, 0, 400, 20);
// pv.center = cell.imageView.center;
// NSLog(@"%ld,%ld",receivedSize,expectedSize);
// [cell.imageView addSubview:pv];
// }
// if(pv)
// {
// pv.progress = currentProgress;
// if(currentProgress >= 1)
// {
// [pv removeFromSuperview];
// }
// }
});
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if(image && !error)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[[SDImageCache sharedImageCache] storeImage:image forKey:imagePath toDisk:YES completion:^{
}];
});
}
}];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
__block NSString *name = [[imagePath lastPathComponent] stringByDeletingPathExtension];
NSString *thumpPathName = [@"thumb" stringByAppendingString:name];
NSString *lastPath = [[imagePath stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", thumpPathName, [imagePath pathExtension]];
__block UIActivityIndicatorView *activityIndicator;
// __block UIProgressView *pv;
// UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:lastPath];
[[SDImageCache sharedImageCache] queryCacheOperationForKey:lastPath done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {//存在缩略图则用缩略图占位
if(image)
{
NSLog(@"图片disk中不存在缩略图,加载远程图片%@,----不加载远程缩略图图片%@",imagePath,lastPath);
}else{
NSLog(@"图片disk中不存在缩略图,加载远程图片%@",imagePath);
}
SDWebImageOptions newOptions = SDWebImageRetryFailed | SDWebImageLowPriority | SDWebImageProgressiveLoad |SDWebImageAvoidDecodeImage ;
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:image?image:self.placeholderImage options:newOptions context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
dispatch_main_sync_safe(^{
// your code
float currentProgress = (float)receivedSize/(float)expectedSize;
if (!activityIndicator)
{
[cell.imageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
activityIndicator.center = cell.imageView.center;
[activityIndicator startAnimating];
}
NSLog(@"当前%f",currentProgress);
if(currentProgress >= 1)
{
if(activityIndicator)
{
// [activityIndicator removeFromSuperview];
[ activityIndicator stopAnimating ];//停止
}
}
// if(!pv)
// {
// pv = [[UIProgressView alloc]initWithProgressViewStyle:(UIProgressViewStyleDefault)];
// pv.frame = CGRectMake(0, 0, 400, 20);
// pv.center = cell.imageView.center;
// NSLog(@"%ld,%ld",receivedSize,expectedSize);
// [cell.imageView addSubview:pv];
// }
// if(pv)
// {
// pv.progress = currentProgress;
// if(currentProgress >= 1)
// {
// [pv removeFromSuperview];
// }
// }
});
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if(image && !error)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//image = [image croppedImage:...]; // 后台线程剪裁图片
[[SDImageCache sharedImageCache] storeImage:image forKey:imagePath toDisk:YES completion:^{
}];
if(![name containsString:@"thumb"])//未生成缩略图
{
UIImage* thumbimage = [UIImage thumbnailWithImageWithoutScale:image size:CGSizeMake(10, 10)]; // 后台线程生成缩略图-->图片
[[SDImageCache sharedImageCache] storeImage:thumbimage forKey:lastPath toDisk:YES completion:^{
}];
//[[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:imagePath];
}
});
}
}];
}];
});
}
}];