+(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; }
时间: 2024-10-29 00:15:29