iOS开发 UIimage旋转,得到旋转后的Image图片,解决imageView旋转,内部图片不跟着旋转问题

一:前言

 1.0  : 图片处理中,比如对一个ImageView进行旋转,那么旋转后图片缺还是原来的样子,本文就是解决图片旋转的问题
 1.0 :无demo说话不硬气,上github地址:https://github.com/horisea/PictureWhitening    
           欢迎star,你的星星是我持续创作的动力
二:上效果图
旋转前iOS开发 UIimage旋转,得到旋转后的Image图片,解决imageView旋转,内部图片不跟着旋转问题_第1张图片旋转后iOS开发 UIimage旋转,得到旋转后的Image图片,解决imageView旋转,内部图片不跟着旋转问题_第2张图片
三:直接上代码:
/**
 *  return 旋转后的图片
 *  @param image              原始图片    (必传,不可空)
 *  @param orientation        旋转方向    (必传,不可空)
 */
+ (UIImage *)image:(UIImage *)image
          rotation:(UIImageOrientation)orientation ;
参数解释:
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 *)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;
}

如果你喜欢这篇文章,或者有任何疑问,可以扫描第一个二维码,加楼主好友哦

也可以扫第二个二维码,关注楼主个人微信公众号。这里有很多生活,职业,技术相关的文章哦。欢迎您的到来。

微信号:                                             公众号


最后:然后你就能得到旋转后的图片了,,

你可能感兴趣的:(iOS,图像处理,ios开发,图片旋转,image旋转,图片处理)