iOS对图像进行压缩代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = image.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;

 if (width <= newSize.width && height <= newSize.height){
  return image;
 }

 if (width == 0 || height == 0){
  return image;
 }

 CGFloat widthFactor = newSize.width / width;
 CGFloat heightFactor = newSize.height / height;
 CGFloat scaleFactor = (widthFactor<heightFactor?widthFactor:heightFactor);

 CGFloat scaledWidth = width * scaleFactor;
 CGFloat scaledHeight = height * scaleFactor;
 CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);

    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:iOS实现圆角效果

下一篇:android读取sim联系人资料的代码