SDWebImage特性之缩略图解码

2,732 阅读1分钟

问题

浏览消息时发现一条有意思的Issues,某猿在开发中发现加载大图时app奔溃了,而这张图片在模拟器加载消耗的内存高大1.6G,最终导致加载在iPhone11上时闪退。

解决方案

通过缩略图解码,可以自定义设置加载图片的大小,防止OOM。

代码实例

  • Objective-C
CGSize thumbnailSize = CGSizeMake(200, 200); // Thumbnail will bounds to (200,200)
[imageView sd_setImageWithURL:url placeholderImage:nil options:0 context:@{SDWebImageContextImageThumbnailPixelSize : @(thumbnailSize)];
  • Swift
let thumbnailSize = CGSize(width: 200, height: 200) // Thumbnail will bounds to (200,200)
imageView.sd_setImage(with: url, placeholderImage: nil, context: [.imageThumbnailPixelSize : thumbnailSize])

更多