定义缩放图片的Category
-
-
// UIImage+Scale.h
-
-
@interface UIImage (scale )
-
-
- (UIImage* )scaleToSize: (CGSize )size;
-
-
@end
-
实现这个Category的定义
-
-
// UIImage+Scale.h
-
-
#import "UIImage+Scale.h"
-
-
@implementation UIImage (scale )
-
-
- (UIImage* )scaleToSize: (CGSize )size
-
{
-
// 创建一个bitmap的context
-
// 并把它设置成为当前正在使用的context
-
UIGraphicsBeginImageContext (size );
-
-
// 绘制改变大小的图片
-
[self drawInRect:CGRectMake ( 0, 0, size.width, size.height ) ];
-
-
// 从当前context中创建一个改变大小后的图片
-
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext ( );
-
-
// 使当前的context出堆栈
-
UIGraphicsEndImageContext ( );
-
-
// 返回新的改变大小后的图片
-
return scaledImage;
-
}
-
-
@end
-
如何使用
-
-
// 创建图片
-
UIImage *image = [UIImage imageNamed:@ "myImage.png" ];
-
-
// 更改图片大小
-
UIImage *scaledImage = [image scaleToSize:CGSizeMake ( 25.0f, 35.0f ) ];
-