ios等比例壓縮圖片
本文由 zqdreamer 發(fā)表于 2014年9月20日
+(UIImage *)compressImageWith:(UIImage *)image width:(float)width height:(float)height
{
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float widthScale = imageWidth /width;
float heightScale = imageHeight /height;
// 創(chuàng)建一個位圖上下文,并把它設置為當前正在使用的上下文
UIGraphicsBeginImageContext(CGSizeMake(width, height));
if (widthScale > heightScale) {//設置壓縮比例
[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
}
else {
[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
}
// 從當前context中創(chuàng)建一個改變大小后的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
[newImage retain];
// 使當前的context出堆棧
UIGraphicsEndImageContext();
return newImage;
}