BeijingLeo

 

[转]截取部分图像+等比例缩放

 

原文地址:http://lulala.blog.51cto.com/780501/659018

 

@interface UIImage(UIImageScale)
-(UIImage*)getSubImage:(CGRect)rect;
-(UIImage*)scaleToSize:(CGSize)size;
@end
 
@implementation UIImage(UIImageScale)
 
//截取部分图像
-(UIImage*)getSubImage:(CGRect)rect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
CGRect smallBounds = CGRectMake(00CGImageGetWidth(subImageRef),CGImageGetHeight(subImageRef));
 
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
 
    return smallImage;
}
 
创建一个bitmap并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
 
    // 从当前context使当前的context    // 返回新的改变大小后的图片  
    return scaledImage;
}
@end
 
 
可以直接用的,你懂的
 

posted on 2012-03-21 13:27  BeijingLeo  阅读(286)  评论(0)    收藏  举报

导航