android 图库 imgcache.idx,iOS开发 - 关于列表图片渲染内存暴增问题

关于列表图片渲染内存暴增问题

- (void)viewDidLoad {

[super viewDidLoad];

[SDImageCache sharedImageCache].config.shouldDecompressImages = NO;

[SDWebImageDownloader sharedDownloader].shouldDecompressImages = NO;

}

[SDImageCache sharedImageCache].config.shouldDecompressImages = NO;

[SDWebImageDownloader sharedDownloader].shouldDecompressImages = NO;

- (void)dealloc {

[SDImageCache sharedImageCache].config.shouldDecompressImages = YES;

[SDWebImageDownloader sharedDownloader].shouldDecompressImages = YES;

[[SDImageCache sharedImageCache] clearMemory];

[[SDWebImageManager sharedManager] cancelAll];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

NSLog(@"当前内存警告,执行缓存清理");

[[SDImageCache sharedImageCache] clearMemory];

[[SDWebImageManager sharedManager] cancelAll];

}

在图片预览下载时进行小图渲染

- (void)setPhotos:(NSArray *)photos

{

_photos = photos;

if (_photos.count > 9) {

_photos = [_photos subarrayWithRange:NSMakeRange(0, 9)];

}

for (long i = _photos.count; i < self.imageViewsArray.count; i++) {

UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];

imageView.hidden = YES;

}

if (_photos.count == 0) {

self.height = 0;

self.fixedHeight = @(0);

return;

}

// Modify By Candy - 方法1

CGFloat itemW = AAdaption(200.0);

CGFloat itemH = 0;

if (_photos.count == 1) {

YSTimeLinePhoto *photo = _photos.firstObject;

itemW = photo.width > itemW ? itemW : photo.width;

itemH = photo.height / photo.width * itemW;

}else {

itemW = (kScreenWidth-80)/3;//AAdaption(70.0);

itemH = itemW;

}

long perRowItemCount = [self perRowItemCountForPicPathArray:_photos];

CGFloat margin = 5;

_photoUrls = [NSMutableArray array];

kWeakSelf

[_photos enumerateObjectsUsingBlock:^(YSTimeLinePhoto * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSString *urlStr = [NSString stringWithFormat:@"%@%@", MAIN_URL, obj.imagePath];

[_photoUrls addObject:urlStr];

long columnIndex = idx % perRowItemCount;

long rowIndex = idx / perRowItemCount;

UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];

imageView.hidden = NO;

imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);

__weak UIImageView *weakImgView = imageView;

[imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:kPlaceholderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

if (!image) return;

weakImgView.image = kPlaceholderImage;

CGSize newSize = image.size;

CGFloat scale = 1.0;

if (newSize.width < 2000) {

scale = 0.7;

}else if (newSize.width < 3000) {

scale = 0.55;

}else if (newSize.width < 4000) {

scale = 0.4;

}else if (newSize.width <= kScreenWidth) {

scale = 1.0;

}else {

scale = 0.3;

}

newSize = CGSizeMake(newSize.width*scale, newSize.height*scale);

///**

// 异步执行任务创建方法

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// 这里放异步执行任务代码

UIImage *newImage = [weakSelf imageResize:image andResizeTo:newSize];

dispatch_async(dispatch_get_main_queue(), ^{

// 回到主线程更新UI

weakImgView.image = newImage;

});

});

//*/

}];

}];

CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;

int columnCount = ceilf(_photos.count * 1.0 / perRowItemCount);

CGFloat h = columnCount * itemH + (columnCount - 1) * margin;

self.width = w;

self.height = h;

self.fixedHeight = @(h);

self.fixedWidth = @(w);

}

// 重新渲染图片显示到列表视图上

- (UIImage *)imageResize:(UIImage*)img andResizeTo:(CGSize)newSize

{

//UIGraphicsBeginImageContext(newSize);

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.5);

[img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

其他解决列表图片卡顿问题方法

2a4594a99e4c

tableView在滚动并且图片未下载则直接加载占位图

2a4594a99e4c

列表滚动触发

2a4594a99e4c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值