修改图片尺寸

@interface UIImage (Category)
- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height;
@end
@implementation UIImage (Category)
- (UIImage*)transformWidth:(CGFloat)width 
height:(CGFloat)height {

CGFloat destW = width;
CGFloat destH = height;
CGFloat sourceW = width;
CGFloat sourceH = height;
    
CGImageRef imageRef = self.CGImage;
CGContextRef bitmap = CGBitmapContextCreate(NULL, 
destW, 
destH,
CGImageGetBitsPerComponent(imageRef), 
4*destW, 
CGImageGetColorSpace(imageRef),
(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));

CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);

CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImageimageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
@end
+(UIImage*)compressImageDownToPhoneScreenSize:(UIImage*)theImage{
UIImage * bigImage = theImage;
float actualHeight = bigImage.size.height;
float actualWidth = bigImage.size.width;

float imgRatio = actualWidth / actualHeight;
float maxRatio = 480.0 / 640;

if(imgRatio != maxRatio ){
if(imgRatio < maxRatio){
imgRatio = 480.0 / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = 480.0;

} else {
imgRatio = 320.0 / actualWidth;
actualHeight = imgRatio * actualHeight;     
actualWidth = 320.0;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[bigImage drawInRect:rect];  // scales image to rect
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return theImage;
}
原文地址:https://www.cnblogs.com/appwgh/p/2517485.html