iOS压缩图片分辨率代码

jopen 10年前

+ (UIImage *)imageWithMaxSide:(CGFloat)length sourceImage:(UIImage *)image{      CGFloat scale = [[UIScreen mainScreen] scale];      CGSize imgSize = CWSizeReduce(image.size, length);      UIImage *img = nil;         UIGraphicsBeginImageContextWithOptions(imgSize, YES, scale);  // 创建一个 bitmap context         [image drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)              blendMode:kCGBlendModeNormal alpha:1.0];              // 将图片绘制到当前的 context 上         img = UIGraphicsGetImageFromCurrentImageContext();            // 从当前 context 中获取刚绘制的图片       UIGraphicsEndImageContext();         return img;}static inlineCGSize CWSizeReduce(CGSize size, CGFloat limit)   // 按比例减少尺寸{      CGFloat max = MAX(size.width, size.height);      if (max < limit) {          return size;      }         CGSize imgSize;      CGFloat ratio = size.height / size.width;         if (size.width > size.height) {          imgSize = CGSizeMake(limit, limit*ratio);      } else {          imgSize = CGSizeMake(limit/ratio, limit);      }         return imgSize;}