// // UIImage+resize.m // test23 // // Created by apple on 15/12/1. // Copyright (c) 2015年 JCZ. All rights reserved. // #import "UIImage+resize.h" @implementation UIImage(resize) //将JPEG格式转换为PNG -(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 - (UIImage *)scaledToSize:(CGSize)newSize { //首先要找到缩放比 按长的适配 不足的部分空白 CGFloat rate =newSize.width*1.0/ self.size.width ; if (self.size.height* rate > newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); // Create a graphics image context UIGraphicsBeginImageContext(size); // new size [self drawInRect:CGRectMake(0,0,size.width,size.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } - (UIImage *)scaledAndCutToSize:(CGSize)newSize{ //首先要找到缩放比 按短的适配 长的部分裁减掉 CGFloat rate =newSize.width*1.0/ self.size.width ; if (self.size.height* rate < newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0,0,size.width,size.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } @end