iOS对图像进行压缩代码详解手机开发

- (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; 
} 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5236.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论