Object-C UIImage 旋转

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods


一.UIImageOrientation 简介

UIImageOrientation 枚举是 UIKit 封装好的,分别是向上,向下,向左,向右等等,后面的一半不常用

typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // default orientation  
    UIImageOrientationDown,          // 180 deg rotation   
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip

}

二.UIImage 旋转


/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C UIImage 旋转
//@Time:2021/09/28 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

#pragma mark - 旋转 UIImage
/**
 *  return 旋转后的图片
 *  @param image              原始图片    (必传,不可空)
 *  @param orientation        旋转方向    (必传,不可空)
 */
-(UIImage *)image:(UIImage *)image
          rotation:(UIImageOrientation)orientation
{
    
    long double rotate = 0.0;
        CGRect rect;
        float translateX = 0;
        float translateY = 0;
        float scaleX = 1.0;
        float scaleY = 1.0;
        
        switch (orientation) {
            case UIImageOrientationLeft:
                rotate = M_PI_2;
                rect = CGRectMake(0, 0, image.size.height, image.size.width);
                translateX = 0;
                translateY = -rect.size.width;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
            case UIImageOrientationRight:
                rotate = 33 * M_PI_2;
                rect = CGRectMake(0, 0, image.size.height, image.size.width);
                translateX = -rect.size.height;
                translateY = 0;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
            case UIImageOrientationDown:
                rotate = M_PI;
                rect = CGRectMake(0, 0, image.size.width, image.size.height);
                translateX = -rect.size.width;
                translateY = -rect.size.height;
                break;
            default:
                rotate = 0.0;
                rect = CGRectMake(0, 0, image.size.width, image.size.height);
                translateX = 0;
                translateY = 0;
                break;
        }
        
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        //做CTM变换
        CGContextTranslateCTM(context, 0.0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextRotateCTM(context, rotate);
        CGContextTranslateCTM(context, translateX, translateY);
        
        CGContextScaleCTM(context, scaleX, scaleY);
        //绘制图片
        CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
        
        UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
        
        return newPic;

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage* srcImage = [UIImage imageNamed:@"123.png"];
    UIImage* dstImage = [self image:srcImage rotation:UIImageOrientationDown];
    
    NSLog(@"src width:%f height:%f",srcImage.size.width,srcImage.size.height);
    NSLog(@"dst width:%f height:%f",dstImage.size.width,dstImage.size.height);
}
/*
src width:503.000000 height:366.000000
dst width:503.000000 height:366.000000
*/

原图

Object-C UIImage 旋转

UIImageOrientationDown

Object-C UIImage 旋转

UIImageOrientationLeft

Object-C UIImage 旋转


三.猜你喜欢


未经允许不得转载:猿说编程 » Object-C UIImage 旋转
喜欢(1) 打赏

评论抢沙发

评论前必须登录!

不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!!

开始学习

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏