SDWebImage 加载多张大图优化

实现方案来自度娘
1.在SDWebImage目录下的 UIImage+MultiFormat.m 文件中增加方法
+ (UIImage *)compressImageWith:(UIImage *)image {
  float imageWidth = image.size.width;
  float imageHeight = image.size.height;
  float width = 640;
  float height = image.size.height/(image.size.width/width);
  float widthScale = imageWidth /width;
  float heightScale = imageHeight /height;
  // 创建一个bitmap的context
  // 并把它设置成为当前正在使用的context
  UIGraphicsBeginImageContext(CGSizeMake(width, height));
  if (widthScale > heightScale) {
    [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
   } else {
    [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
   }
  // 从当前context中创建一个改变大小后的图片
  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  // 使当前的context出堆栈
  UIGraphicsEndImageContext();
  return newImage;
}
2.在该文件下面方法中调用
+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {
  if (!data) { return nil; }
  UIImage *image;
  SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:data];
  if (imageFormat == SDImageFormatGIF) {
    image = [UIImage sd_animatedGIFWithData:data];
  }
#ifdef SD_WEBP
  else if (imageFormat == SDImageFormatWebP) {
    image = [UIImage sd_imageWithWebPData:data];
  }
#endif
  else {
  image = [[UIImage alloc] initWithData:data];
  //在这里增加下面三行代码
  if (data.length/1024 > 128) {
    image = [self compressImageWith:image];
  }
#if SD_UIKIT || SD_WATCH
  UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];
  if (orientation != UIImageOrientationUp) {
    image = [UIImage imageWithCGImage:image.CGImage
    scale:image.scale orientation:orientation];
  }
#endif
  }
  return image;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,142评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • 公众号:虚度时光 10多年没有交集的高中同学建群突然有联系了,是不是立马热血起来,7、8 年没联系的大学同学建群...
    虚度时光读吧阅读 190评论 0 0
  • 一切都是虚妄! 我们时而怀念的 冬日清晨的那层薄霜 秋风里旋转飞舞的黄叶 仲夏夜晚的虫鸣蛙声 还有春天丝毛般抚摸的...
    三月无名阅读 153评论 0 1
  • 日记写了这么些天,好像已经习惯了,虽然写的不好,但是它记录了每天发生的事,记录了我的期望,我的改变,孩子的改变,还...
    梅美洁车饰阅读 145评论 0 0