iOS对图像进行压缩代码
2018-07-20 来源:open-open
- (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实现圆角效果
最新资讯
热门推荐