//对图片尺寸进行压缩--
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
// 对图片大小进行压缩,压缩到不大于size(字节)
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFileSize:(CGFloat)size{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if (size < imageData.length) {
CGFloat compressionQuality = size / imageData.length;
NSData *newImageData = UIImageJPEGRepresentation(image, compressionQuality);
UIImage *newImage = [UIImage imageWithData:newImageData];
return newImage;
}
return image;
}