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;
}

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

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