将一幅图片按着需要的尺寸进行等比的压缩和放大,最后再截取需要尺寸部分,不知道说清楚没,反正就那意思吧!
+(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; // 创建一个bitmap的context // 并把它设置成为当前正在使用的context 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中创建一个改变大小后的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); [newImage retain]; // 使当前的context出堆栈 UIGraphicsEndImageContext(); return newImage; }
新闻热点
疑难解答