转:https://www.jianshu.com/p/f884bfba1c28

 

iOS拍照后图片自动翻转,是Image有一个imageOrientation属性,里面有图片拍照的方法,只要在拍照后的代理方法中,将返回的图片在翻转回来就可以了。

代码如下:

// 判断图片是否翻转
- (UIImage *)fixOrientation:(UIImage *)aImage {
    
    // No-op if the orientation is already correct
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    
    // 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 (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

作者:爱豆一颗心
链接:https://www.jianshu.com/p/f884bfba1c28
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 
 
 

iOS UIImage 图片旋转

 

转:https://www.jianshu.com/p/f17330ea7a28

 

最近因项目中缺少ios开发,临时客串解决一些ios问题。
在一个需求中,获取到照片的数据,将UIImage写入文件,发现生成的图片是斜的。
朝向向右,而不是向上。
而用UIImageView时能显示正常,因为UIImageView会根据图片旋转容器的方向。
而我们是要把图片上传到服务器,所以这个不行。

然后在网上找到的办法如下:(不行)

+ (UIImage *)fixOrientation:(UIImage *)aImage {

    

    // No-op if the orientation is already correct

    if (aImage.imageOrientation ==UIImageOrientationUp)

        return aImage;

    

    // 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 (aImage.imageOrientation) {

        caseUIImageOrientationDown:

        caseUIImageOrientationDownMirrored:

            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);

            transform = CGAffineTransformRotate(transform, M_PI);

            break;

            

        caseUIImageOrientationLeft:

        caseUIImageOrientationLeftMirrored:

            transform = CGAffineTransformTranslate(transform, aImage.size.width,0);

            transform = CGAffineTransformRotate(transform, M_PI_2);

            break;

            

        caseUIImageOrientationRight:

        caseUIImageOrientationRightMirrored:

            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);

            transform = CGAffineTransformRotate(transform, -M_PI_2);

            break;

        default:

            break;

    }

    

    switch (aImage.imageOrientation) {

        caseUIImageOrientationUpMirrored:

        caseUIImageOrientationDownMirrored:

            transform = CGAffineTransformTranslate(transform, aImage.size.width,0);

            transform = CGAffineTransformScale(transform, -1, 1);

            break;

            

        caseUIImageOrientationLeftMirrored:

        caseUIImageOrientationRightMirrored:

            transform = CGAffineTransformTranslate(transform, aImage.size.height,0);

            transform = CGAffineTransformScale(transform, -1, 1);

            break;

        default:

            break;

    }

    

    // Now we draw the underlying CGImage into a new context, applying the transform

    // calculated above.

    CGContextRef ctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,

                                             CGImageGetBitsPerComponent(aImage.CGImage),0,

                                             CGImageGetColorSpace(aImage.CGImage),

                                             CGImageGetBitmapInfo(aImage.CGImage));

    CGContextConcatCTM(ctx, transform);

    switch (aImage.imageOrientation) {

        caseUIImageOrientationLeft:

        caseUIImageOrientationLeftMirrored:

        caseUIImageOrientationRight:

        caseUIImageOrientationRightMirrored:

            // Grr...

            CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);

            break;

            

        default:

            CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);

            break;

    }

    

    // And now we just create a new UIImage from the drawing context

    CGImageRef cgimg =CGBitmapContextCreateImage(ctx);

    UIImage *img = [UIImageimageWithCGImage:cgimg];

    CGContextRelease(ctx);

    CGImageRelease(cgimg);

    return img;

}

 

该方法不行,在断点调试时发现,是因为该图片UIImage的朝向就是 UIImageOrientationUp。
也就是在已进入这个方法时就return了,下面的都没有执行。
但我们视觉看到的确实是朝向不对,代码检测的却是朝向上。

所以导致该方法不行,我试过去屏蔽掉第一句的判断以及return。但是生成的图片白屏。

还是过其他的办法,发现有的是能旋转过来,但图片的宽高会有问题,导致图片在宽度上被拉伸,高度上被压缩,
即图片的宽高没有对换。

然后发现一个方法: quartz2D来画图片,然后使用ctm变幻来实现旋转

原文地址:https://blog.csdn.net/bitcser/article/details/52055442?utm_source=blogxgwz4

代码如下:

+(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 =3 *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;

}

 

作者:justin_crashed
链接:https://www.jianshu.com/p/f17330ea7a28
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 
 
 
 其他相关类似:

ios 图片逆时针旋转_iphone-IOS 竖直拍照被旋转,image-orientation 让图片自动旋转 https://blog.csdn.net/weixin_35975177/article/details/111983610

 手机竖屏拍照图片旋转90度问题解决方法 https://wenku.baidu.com/view/804e3c09b7daa58da0116c175f0e7cd1842518c2.html?_wkts_=1667791230742

iOS 11.0 之后拍照生成的imageOrientation的问题 https://cloud.tencent.com/developer/article/1813873

 
 
 
 
 
 
 
 
 
 
 
版权声明:本文为LiuZX90原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/LiuZX90/p/16386578.html