美文网首页
SDWebImage 加载多张大图优化

SDWebImage 加载多张大图优化

作者: 今晚打老虎_9527 | 来源:发表于2019-02-27 14:10 被阅读0次
    实现方案来自度娘
    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;
    }
    

    相关文章

      网友评论

          本文标题:SDWebImage 加载多张大图优化

          本文链接:https://www.haomeiwen.com/subject/sqxfuqtx.html