调整图片的尺寸

头文件:

 

Ios代码   收藏代码
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface UIImage (Resize)  
  4.   
  5. - (UIImage *)transformWidth:(CGFloat)width height:(CGFloat)height;  
  6.   
  7. @end  

 

实现文件:

 

Ios代码   收藏代码
  1. #import "ImageResize.h"  
  2.   
  3. @implementation UIImage (Resize)  
  4.   
  5. - (UIImage *)transformWidth:(CGFloat)width height:(CGFloat)height {   
  6.     CGFloat destW = width;  
  7.     CGFloat destH = height;  
  8.     CGFloat sourceW = width;  
  9.     CGFloat sourceH = height;  
  10.       
  11.     CGImageRef imageRef = self.CGImage;  
  12.     CGContextRef bitmap = CGBitmapContextCreate(NULL, destW, destH,  
  13.                                                                                             CGImageGetBitsPerComponent(imageRef),   
  14.                                                                                             4 * destW, CGImageGetColorSpace(imageRef),  
  15.                                                                                             (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));  
  16.       
  17.     CGContextDrawImage(bitmap, CGRectMake(00, sourceW, sourceH), imageRef);  
  18.       
  19.     CGImageRef ref = CGBitmapContextCreateImage(bitmap);  
  20.     UIImage *result = [UIImage imageWithCGImage:ref];  
  21.     CGContextRelease(bitmap);  
  22.     CGImageRelease(ref);  
  23.       
  24.     return result;  
  25. }  
  26.   
  27. @end  

 

示例:

 

Ios代码   收藏代码
  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];  
  2. UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];  
  3. image = [image transformWidth:100.f height:100.f];  
  4. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];  
  5. [image release];  
  6. [self.view addSubview:imageView];  
  7. [imageView release];  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值