美文网首页
iOS 图片压缩

iOS 图片压缩

作者: Finger_s | 来源:发表于2018-10-31 16:44 被阅读0次
    • (UIImage )zipNSDataWithImage:(UIImage )sourceImage{
      //进行图像尺寸的压缩
      CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
      CGFloat width = imageSize.width; //图片宽度
      CGFloat height = imageSize.height; //图片高度
      //1.宽高大于1280(宽高比不按照2来算,按照1来算)
      if (width>1280||height>1280) {
      if (width>height) {
      CGFloat scale = height/width;
      width = 1280;
      height = width
      scale;
      }else{
      CGFloat scale = width/height;
      height = 1280;
      width = height
      scale;
      }
      //2.宽大于1280高小于1280
      }else if(width>1280||height<1280){
      CGFloat scale = height/width;
      width = 1280;
      height = widthscale;
      //3.宽小于1280高大于1280
      }else if(width<1280||height>1280){
      CGFloat scale = width/height;
      height = 1280;
      width = height
      scale;
      //4.宽高都小于1280
      }else{
      }
      UIGraphicsBeginImageContext(CGSizeMake(width, height));
      [sourceImage drawInRect:CGRectMake(0,0,width,height)];
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

      //进行图像的画面质量压缩
      NSData data=UIImageJPEGRepresentation(newImage, 1.0);
      if (data.length>100
      1024) {
      if (data.length>10241024) {//1M以及以上
      data=UIImageJPEGRepresentation(newImage, 0.1);
      }else if (data.length>512
      1024) {//0.5M-1M
      data=UIImageJPEGRepresentation(newImage, 0.2);
      }else if (data.length>200*1024) {
      //0.25M-0.5M
      data=UIImageJPEGRepresentation(newImage, 0.3);
      }
      }
      return [UIImage imageWithData:data];
      }

    相关文章

      网友评论

          本文标题:iOS 图片压缩

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