UIImage 旋转 镜像 rotation mirror

 
 
来自: 点击打开链接  
// 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值