UIImage得一个Category, Resizes(调整尺寸)

来源:互联网 发布:监控平台用什么编程 编辑:程序博客网 时间:2024/06/05 20:36

写在最前边: Category得用法很方便, 举例来说: 新建一个Class文件, 文件名随意需要建立UIImage得Category就按照如下格式添加函数,括号内名称随意, 这种格式, 编译器即认为是一个Category
@interface UIImage (Category)/** Resizes and/or rotates an image.*/- (UIImage*)transformWidth:(CGFloat)width                                   height:(CGFloat)height;@end @implementation UIImage (Category) - (UIImage*)transformWidth:(CGFloat)width                     height:(CGFloat)height {     CGFloat destW = width;    CGFloat destH = height;    CGFloat sourceW = width;    CGFloat sourceH = height;     CGImageRef imageRef = self.CGImage;    CGContextRef bitmap = CGBitmapContextCreate(NULL,                                                 destW,                                                 destH,                                                CGImageGetBitsPerComponent(imageRef),                                                 4*destW,                                                 CGImageGetColorSpace(imageRef),                                                (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));     CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);     CGImageRef ref = CGBitmapContextCreateImage(bitmap);    UIImage *result = [UIImage imageWithCGImage:ref];    CGContextRelease(bitmap);    CGImageRelease(ref)return result;}@end

原创粉丝点击