压缩图片

/**
压缩图片

@param scImg 图片
@param limitSize 大小

@return 压缩到指定大小
*/
+(UIImage *)thumbImageWithImage:(UIImage *)scImg limitSize:(CGSize)limitSize
{
if (scImg.size.width <= limitSize.width && scImg.size.height <= limitSize.height) {
return scImg;
}
CGSize thumbSize;
if (scImg.size.width / scImg.size.height > limitSize.width / limitSize.height) {
thumbSize.width = limitSize.width;
thumbSize.height = limitSize.width / scImg.size.width * scImg.size.height;
}
else {
thumbSize.height = limitSize.height;
thumbSize.width = limitSize.height / scImg.size.height * scImg.size.width;
}
UIGraphicsBeginImageContext(thumbSize);
[scImg drawInRect:(CGRect){CGPointZero,thumbSize}];
UIImage *thumbImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumbImg;
}

你可能感兴趣的:(压缩图片)