使用分类向某个现有类添加方法(类扩展,范畴)
2013-07-03 17:01
288 查看
iOS中有时候我们需要向现有的类中添加一些方法,使用分类(Category)可以在运行时向某个现有类添加新的方法,这在我们向系统类或者我们不知道源码的类添加我们需要的方法时,非常的方便。不过分类有个缺点,无法添加新的变量,解决方法见下篇。写个Demo,代码如下:
这段代码的作用是修复拍摄图片传到服务器时倒置等情况,使用时,在头文件
#import "UIImage+fixOrientation.h"
然后
这样就可以了,enjoy yourself!
#import <UIKit/UIKit.h> @interface UIImage (fixOrientation) - (UIImage *)fixOrientation; @end
#import "UIImage+fixOrientation.h" @implementation UIImage (fixOrientation) - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; } @end
这段代码的作用是修复拍摄图片传到服务器时倒置等情况,使用时,在头文件
#import "UIImage+fixOrientation.h"
然后
UIImage *img = [UIImage imageNamed:@"tt.png"]; UIImage *theImage = [image fixOrientation];
这样就可以了,enjoy yourself!
相关文章推荐
- macOS 中使用 phpize 动态添加 PHP 扩展的错误解决方法
- 使用扩展方法添加到String类中简化脚本注入处理
- 插件: images and custom options,magento添加多图片,自定义选项custom options,catelog,分类的插件以及使用方法!!!
- 部分类、静态类、Object类、ToString()方法、扩展方法的使用
- HtmlHelper 添加扩展方法. 使用Lambda表达式作为参数
- 编写高质量代码改善C#程序的157个建议——建议101:使用扩展方法,向现有类型“添加”方法
- 使用 Iisext.vbs 添加Web服务扩展文件的方法
- 插件: images and custom options,magento添加多图片,自定义选项custom options,catelog,分类的插件以及使用方法!!!
- 使用扩展方法和接口给对象添加“重置状态”功能
- 使用扩展方法,向现有类型“添加”方法
- CSS扩展选择器的分类及使用方法
- Scala中使用implict 扩展现有类的方法
- 建议101:使用扩展方法,向现有类型“添加”方法
- 使用分类为数组添加快速排序方法和折半查找方法
- 使用libzip读取修改zip文件comment的方法(支持添加扩展字符)
- 为类库添加扩展方法的使用实例
- HtmlHelper 添加扩展方法. 使用Lambda表达式作为参数
- macOS 中使用 phpize 动态添加 PHP 扩展的错误解决方法
- 在word文档中添加目录(包括项目符号、编号、多级列表使用方法)
- 使用 MSIL 为 Enum.HasFlag 封装一个泛型的扩展方法