123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //
- // UIImage+Rotate.m
- // UIImage+Categories
- //
- // Created by lisong on 16/9/4.
- // Copyright © 2016年 lisong. All rights reserved.
- //
- #import "UIImage+Rotate.h"
- //由角度转换弧度
- #define kDegreesToRadian(x) (M_PI * (x) / 180.0)
- //由弧度转换角度
- #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)
- @implementation UIImage (Rotate)
- /** 纠正图片的方向 */
- - (UIImage *)fixOrientation
- {
- 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;
- }
-
- switch (self.imageOrientation)
- {
- case UIImageOrientationUpMirrored:
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.width, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
-
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRightMirrored:
- transform = CGAffineTransformTranslate(transform, self.size.height, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- case UIImageOrientationUp:
- case UIImageOrientationDown:
- case UIImageOrientationLeft:
- case UIImageOrientationRight:
- 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:
- 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;
- }
-
- CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
- UIImage *img = [UIImage imageWithCGImage:cgimg];
- CGContextRelease(ctx);
- CGImageRelease(cgimg);
-
- return img;
- }
- /** 按给定的方向旋转图片 */
- - (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:
- return self;
-
- 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:
- return self;
- }
-
- 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;
- }
- /** 垂直翻转 */
- - (UIImage *)flipVertical
- {
- return [self rotate:UIImageOrientationDownMirrored];
- }
- /** 水平翻转 */
- - (UIImage *)flipHorizontal
- {
- return [self rotate:UIImageOrientationUpMirrored];
- }
- /** 将图片旋转弧度radians */
- - (UIImage *)imageRotatedByRadians:(CGFloat)radians
- {
- // calculate the size of the rotated view's containing box for our drawing space
- UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
- CGAffineTransform t = CGAffineTransformMakeRotation(radians);
- rotatedViewBox.transform = t;
- CGSize rotatedSize = rotatedViewBox.frame.size;
-
- // Create the bitmap context
- UIGraphicsBeginImageContext(rotatedSize);
- CGContextRef bitmap = UIGraphicsGetCurrentContext();
-
- // Move the origin to the middle of the image so we will rotate and scale around the center.
- CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
-
- // // Rotate the image context
- CGContextRotateCTM(bitmap, radians);
-
- // Now, draw the rotated/scaled image into the context
- CGContextScaleCTM(bitmap, 1.0, -1.0);
- CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
-
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return newImage;
- }
- /** 将图片旋转角度degrees */
- - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
- {
- return [self imageRotatedByRadians:kDegreesToRadian(degrees)];
- }
- /** 交换宽和高 */
- static CGRect swapWidthAndHeight(CGRect rect)
- {
- CGFloat swap = rect.size.width;
-
- rect.size.width = rect.size.height;
- rect.size.height = swap;
-
- return rect;
- }
- @end
|