欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS拍照后图片自动旋转90度的完美解决方法

时间:2021-01-13 09:59:14|栏目:iOS代码|点击:

今天开发一个拍照获取照片的功能的时候, 发现上传之后图片会自动旋转90.

测试发现, 只要是图片大于2M, 系统就会自动翻转照片

  相机拍照后直接取出来的UIimage(用UIImagePickerControllerOriginalImage取出),它本身的imageOrientation属性是3,即UIImageOrientationRight。如果这个图片直接使用则没事,但是如果对它进行裁剪、缩放等操作后,它的这个imageOrientation属性会变成0。此时这张图片用在别的地方就会发生旋转。imageOrientation是只读的,不能直接修改其值。

解决方法如下:

1. 设置相机的一个属性allowsEditing为YES,设了这个值,你拍完照片后会在照片上出现一个框框,这就是对照片的裁剪编辑。在相机的代理方法中取照片的时候就别用UIImagePickerControllerOriginalImage来取了,要用UIImagePickerControllerEditedImage。用这个key取出来的照片,它的imageOrientation是0,所以之后的任何裁剪、缩放操作都不会造成旋转。这是第一种方法。

2. 第一种解决方法基本没用, 开发中基本都会对图片进行裁剪和压缩. 这里有一个专门针对这个事的很好的category

+ (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;
}

上一篇:IOS文件的简单读写实例详解

栏    目:iOS代码

下一篇:iOS sqlite对数据库的各种操作(日常整理全)

本文标题:iOS拍照后图片自动旋转90度的完美解决方法

本文地址:http://www.codeinn.net/misctech/44757.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有