平时开发中图片的压缩处理一般包括所占内存大小的处理和尺寸的处理,内存方面的处理通常直接调用UIImageJPEGRepresentation(image,f)方法进行处理。但有时候会发现无论我们怎么调小f的值都不能继续压缩图片,也就是说UIImageJPEGRepresentation对图片的压缩是有限度的,这时可以对图片的尺寸进行处理,上代码
+ (UIImage*)scaleFromImage:(UIImage*)sourceImage targetSize:(CGFloat)defineWidth
{
CGSizeimageSize = sourceImage.size;
CGFloatwidth = imageSize.width;
CGFloatheight = imageSize.height;
CGFloattargetWidth = defineWidth;
CGFloattargetHeight = (targetWidth / width) * height;
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
[sourceImagedrawInRect:CGRectMake(0,0,targetWidth,targetHeight)];
UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnnewImage;
}
网友评论