IOS 自定义图片大小(Category方法)

UIImage+scaleToSize.h
#import <UIKit/UIKit.h>

@interface UIImage (scaleToSize)

/*
 *  功能说明:自定义图片大小
 *
 */
+ (UIImage*)OriginImage:(NSString *)imageName scaleToSize:(CGSize)size;

@end

UIImage+scaleToSize.m
#import "UIImage+scaleToSize.h"

@implementation UIImage (scaleToSize)

+ (UIImage*)OriginImage:(NSString *)imageName scaleToSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    UIImage *image = [UIImage imageNamed:imageName];
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}

@end

 

posted @ 2013-05-23 11:10  菜鸟程序猿  阅读(324)  评论(0编辑  收藏  举报