来自: 点击打开链接// UKImage.h -- extra UIImage methods // by allen brunson march 29 2009 #ifndef UKIMAGE_H #define UKIMAGE_H #import <UIKit/UIKit.h> @interface UIImage (UKImage ) - (UIImage * )rotate : (UIImageOrientation )orient; @end #endif // UKIMAGE_H// UKImage.mm -- extra UIImage methods // by allen brunson march 29 2009 // based on original code by Kevin Lohman: // http://blog.logichigh.com/2008/06/05/uiimage-fix/ #include "UKImage.h" static CGRect swapWidthAndHeight (CGRect rect ) { CGFloat swap = rect.size.width; rect.size.width = rect.size.height; rect.size.height = swap; return rect; } @implementation UIImage (UKImage ) - (UIImage * )rotate : (UIImageOrientation )orient { CGRect bnds = CGRectZero; UIImage * copy = nil; CGContextRef ctxt = nil; CGImageRef imag = self.CGImage; CGRect rect = CGRectZero; CGAffineTransform tran = CGAffineTransformIdentity; rect.size.width = CGImageGetWidth (imag ); rect.size.height = CGImageGetHeight (imag ); bnds = rect; switch (orient ) { case UIImageOrientationUp : // would get you an exact copy of the original assert ( false ); return nil; case UIImageOrientationUpMirrored : tran = CGAffineTransformMakeTranslation (rect.size.width, 0.0 ); tran = CGAffineTransformScale (tran, -1.0, 1.0 ); break; case UIImageOrientationDown : tran = CGAffineTransformMakeTranslation (rect.size.width, rect.size.height ); tran = CGAffineTransformRotate (tran, M_PI ); break; case UIImageOrientationDownMirrored : tran = CGAffineTransformMakeTranslation (0.0, rect.size.height ); tran = CGAffineTransformScale (tran, 1.0, -1.0 ); break; case UIImageOrientationLeft : bnds = swapWidthAndHeight (bnds ); tran = CGAffineTransformMakeTranslation (0.0, rect.size.width ); tran = CGAffineTransformRotate (tran, 3.0 * M_PI / 2.0 ); break; case UIImageOrientationLeftMirrored : bnds = swapWidthAndHeight (bnds ); tran = CGAffineTransformMakeTranslation (rect.size.height, rect.size.width ); tran = CGAffineTransformScale (tran, -1.0, 1.0 ); tran = CGAffineTransformRotate (tran, 3.0 * M_PI / 2.0 ); break; case UIImageOrientationRight : bnds = swapWidthAndHeight (bnds ); tran = CGAffineTransformMakeTranslation (rect.size.height, 0.0 ); tran = CGAffineTransformRotate (tran, M_PI / 2.0 ); break; case UIImageOrientationRightMirrored : bnds = swapWidthAndHeight (bnds ); tran = CGAffineTransformMakeScale ( -1.0, 1.0 ); tran = CGAffineTransformRotate (tran, M_PI / 2.0 ); break; default : // orientation value supplied is invalid assert ( false ); return nil; } UIGraphicsBeginImageContext (bnds.size ); ctxt = UIGraphicsGetCurrentContext ( ); switch (orient ) { case UIImageOrientationLeft : case UIImageOrientationLeftMirrored : case UIImageOrientationRight : case UIImageOrientationRightMirrored : CGContextScaleCTM (ctxt, -1.0, 1.0 ); CGContextTranslateCTM (ctxt, -rect.size.height, 0.0 ); break; default : CGContextScaleCTM (ctxt, 1.0, -1.0 ); CGContextTranslateCTM (ctxt, 0.0, -rect.size.height ); break; } CGContextConcatCTM (ctxt, tran ); CGContextDrawImage (UIGraphicsGetCurrentContext ( ), rect, imag ); copy = UIGraphicsGetImageFromCurrentImageContext ( ); UIGraphicsEndImageContext ( ); return copy; } @end
UIImage 旋转 镜像 rotation mirror
最新推荐文章于 2023-12-01 11:42:23 发布