欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-22 09:15:45
今天开发一个拍照获取照片的功能的时候, 发现上传之后图片会自动旋转90. 测试发现, 只要是图片大于2m, 系统就会自动翻转照片   相机拍照后直接取出来的u...

今天开发一个拍照获取照片的功能的时候, 发现上传之后图片会自动旋转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拍照后图片自动旋转90度的完美解决方法,希望对大家有所帮助

上一篇:

下一篇: