Object-C UIImage 旋转

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 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

图片[2]-Object-C UIImage 旋转-猿说编程

UIImageOrientationLeft

图片[3]-Object-C UIImage 旋转-猿说编程


三.猜你喜欢


ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容