ios中图像进行压缩方法汇总

2020-01-12 13:49:54丽君

            {  
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;  
            }  
    }  
    UIGraphicsBeginImageContext(targetSize); // this will crop  
    CGRect thumbnailRect = CGRectZero;  
    thumbnailRect.origin = thumbnailPoint;  
    thumbnailRect.size.width  = scaledWidth;  
    thumbnailRect.size.height = scaledHeight;  
    [sourceImage drawInRect:thumbnailRect];  
    newImage = UIGraphicsGetImageFromCurrentImageContext();  
    if(newImage == nil)  
        NSLog(@"could not scale image");  
    //pop the context to get back to the default  
    UIGraphicsEndImageContext();  
    return newImage;  
}  
@end  

 

方法三:(本人项目中使用的方法)

 

复制代码
-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

 

以上所述就是本文的全部内容了,希望大家能够喜欢。



注:相关教程知识阅读请移步到IOS开发频道。