壓縮 UIImage
UIImage+Scale.h
@interface UIImage (Scale)
//等比率縮放
- (UIImage *)scaleImageToScale:(float)scale;
//自定長寬
- (UIImage*)scaleImageToSize:(CGSize)size;
@end
UIImage+Scale.m
import "UIImage+Scale.h"
@implementation UIImage (Scale)
//等比率縮放
- (UIImage *)scaleImageToScale:(float)scale
{
UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale)); [self drawInRect:CGRectMake(0, 0, self.size.width * scale, self.size.height * scale)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return scaledImage;
}
//自定長寬 - (UIImage)scaleImageToSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return scaledImage;
}
@end