UIImage图片的缩小与放大

来源:互联网 发布:java socket 即时通讯 编辑:程序博客网 时间:2024/05/17 21:45

[zt]UIImage图片的缩小与放大


UIImage可以加载图片,但是我们想要得到一张缩小或放大的图片,利用UIImage不能做到,下面我们添加一个UIImage的分类,用来实现UIImage中图片的放大和缩小。

首先,创建一个UIImage+Scale类。

image

然后,实现该类的方法:

#import <UIKit/UIKit.h> 
@interface UIImage (scale) 
-(UIImage*)scaleToSize:(CGSize)size; 
@end


#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

最后,就是该类的使用了:

#import "UIImage+Scale.h"

[[UIImage imageNamed:”p.png”] scaleToSize:CGSizeMake(252.0f, 192.0f)];

原创粉丝点击