uiimage 缩放

定义缩放图片的Category

  1.  
  2. //  UIImage+Scale.h
  3.  
  4. @interface UIImage  (scale )
  5.  
  6. - (UIImage* )scaleToSize: (CGSize )size;
  7.  
  8. @end
  9.  

实现这个Category的定义

  1.  
  2. //  UIImage+Scale.h
  3.  
  4. #import "UIImage+Scale.h"
  5.  
  6. @implementation UIImage  (scale )
  7.  
  8. - (UIImage* )scaleToSize: (CGSize )size
  9. {
  10.    // 创建一个bitmap的context
  11.    // 并把它设置成为当前正在使用的context
  12.   UIGraphicsBeginImageContext (size );
  13.  
  14.    // 绘制改变大小的图片
  15.    [self drawInRect:CGRectMake ( 00, size.width, size.height ) ];
  16.  
  17.    // 从当前context中创建一个改变大小后的图片
  18.   UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext ( );
  19.  
  20.    // 使当前的context出堆栈
  21.   UIGraphicsEndImageContext ( );
  22.  
  23.    // 返回新的改变大小后的图片
  24.    return scaledImage;
  25. }
  26.  
  27. @end
  28.  

如何使用

  1.  
  2. // 创建图片
  3. UIImage *image =  [UIImage imageNamed:@ "myImage.png" ];
  4.  
  5. // 更改图片大小
  6. UIImage *scaledImage =  [image scaleToSize:CGSizeMake ( 25.0f,  35.0f ) ];
  7.  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值