使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrientationLeft,在使用的时候会出现图片顺时针偏转90°。使用fixOrientation方法修正这个问题。

- (UIImage *)fixOrientation
{
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp)
return self; // 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 (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break; case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break; case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
} switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break; case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.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, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break; default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.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;
}

修正iOS从照相机和相册中获取的图片方向的更多相关文章

  1. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  2. 修正iOS从照相机和相册中获取的图片方向(转)

    - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientati ...

  3. 系统相册中获取gif图片 保证取到的图片不会改变

    NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL];                         ...

  4. open file /var/mobile/Media/DCIM 相册中获取到的视频地址使用 报错 视频文件不存在

    从相册中获取到的视频地址 例如  file:///var/mobile/Media/DCIM/100APPLE/IMG_9876.MOV 后面再使用的时候报错 视频文件不存在  那是因为在ios10. ...

  5. iOS--app自定义相册--从自定义的相册中获取图片

    一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerContr ...

  6. Android中使用OKHttp上传图片,从相机和相册中获取图片并剪切

    效果:注意:1:网络权限<;;;); intent.putExtra(); ); intent.putExtra(); intent.putExtra(, byteArrayOutputStre ...

  7. Swift4.0 从相册中获取图片和拍照

    第一步 添加协议 UIImagePickerControllerDelegate,UINavigationControllerDelegate   第二步 添加选择方式 let sexActionSh ...

  8. Android中获取选择图片与获取拍照返回结果差异

    导语: 如今的安卓应用在选择图片的处理上大多合并使用拍照和从相册中选择这两种方式 今天在写一个这样的功能时遇到一个尴尬的问题,同样是拍照获取图片功能,在不同手机上运行的效果不一样,下面是在某型手机上测 ...

  9. C# 从类库中获取资源图片,把图片资源保存到类库中

    /// <summary> /// 获取资源图片 /// </summary> public class AssemblyHelper { #region 常量 /// < ...

随机推荐

  1. BZOJ4386[POI2015]Wycieczki——矩阵乘法+倍增

    题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入 第 ...

  2. JPQL设置自增长、只读、文本类型等的注解

    JAVA中使用JPQL 一种设置id自动生成,自增长的方法 private long id; @Id @GeneratedValue(generator="_native") @G ...

  3. String类型的特殊之处

    String是一种特殊的引用类型,那么它究竟特殊在哪里? 请看看下面这个程序,输出什么结果? public static void changeStr(String str) { str = &quo ...

  4. Threed.sleep是不会释放锁,而wait是释放锁的(对象锁)

    实战分析 一直都说,Threed.sleep是不会释放锁,而wait是释放锁的(对象锁),现理论上来分析一下啊. v package thread.concurrent; public class D ...

  5. android 让真机显示 DeBug Log调试信息

    真机默认是不开启Log 开关的,这么来说我们如果使用真机来搞程序测试的话,需要做以下几个步骤: 下面以华为手机为例开启手机的log功能:  1.在拨号界面输入:*#*#2846579#*#*  进入测 ...

  6. eclipse中用maven创建web项目

    上一节中完成了本地的maven环境搭建,在eclipse中怎么创建一个maven项目呢 一.eclipse中配置maven环境 eclipse版本4.3 1.打开菜单Help->Eclipse ...

  7. Shell基础知识(六)

    shell中有很多内建命令,如何区分内建命令与外部文件,使用type command即可看到命令类型. >> type cd # input << cd is a Shell ...

  8. Mysql distinct、group by

    具体业务场景:根据某些字段组合去重得到所有字段结果. 遇到的error:sql_mode=only_full_group_by. 原因是mysql配置问题. distinct: distinct这个关 ...

  9. PendingIntent的使用

    1, 构造intent Intent mIntent = new Intent("android.intent.action.MAIN"); ComponentName comp ...

  10. 洛谷P3952 时间复杂度

    大毒瘤...... 时隔快半年我终于花了两个小时堪堪A掉这一题...果然我还没有准备好. 想法:用DFS模拟递归. 时间复杂度的处理:每层循环取max,然后相加. 最大难点:各种繁杂而令人发指的特判. ...