修正iOS从照相机和相册中获取的图片 方向
修正iOS从照相机和相册中获取的图片 方向
修正iOS从照相机和相册中获取的图片 方向
使用系统相机拍照得到的图片的默认方向有时不是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从照相机和相册中获取的图片 方向的更多相关文章
- 修正iOS从照相机和相册中获取的图片方向(转)
- (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientati ...
- 修正iOS从照相机和相册中获取的图片方向
使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrientationLeft,在使用的时候会出现图片顺时针偏转90°.使用fixOrientati ...
- 系统相册中获取gif图片 保证取到的图片不会改变
NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL]; ...
- open file /var/mobile/Media/DCIM 相册中获取到的视频地址使用 报错 视频文件不存在
从相册中获取到的视频地址 例如 file:///var/mobile/Media/DCIM/100APPLE/IMG_9876.MOV 后面再使用的时候报错 视频文件不存在 那是因为在ios10. ...
- iOS--app自定义相册--从自定义的相册中获取图片
一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerContr ...
- Android中使用OKHttp上传图片,从相机和相册中获取图片并剪切
效果:注意:1:网络权限<;;;); intent.putExtra(); ); intent.putExtra(); intent.putExtra(, byteArrayOutputStre ...
- Swift4.0 从相册中获取图片和拍照
第一步 添加协议 UIImagePickerControllerDelegate,UINavigationControllerDelegate 第二步 添加选择方式 let sexActionSh ...
- Android中获取选择图片与获取拍照返回结果差异
导语: 如今的安卓应用在选择图片的处理上大多合并使用拍照和从相册中选择这两种方式 今天在写一个这样的功能时遇到一个尴尬的问题,同样是拍照获取图片功能,在不同手机上运行的效果不一样,下面是在某型手机上测 ...
- C# 从类库中获取资源图片,把图片资源保存到类库中
/// <summary> /// 获取资源图片 /// </summary> public class AssemblyHelper { #region 常量 /// < ...
随机推荐
- UVa-1586-分子量
这是一道字符串的题目,我们直接对字符串进行解析,然后计算就可以了. 我是直接开了两个数组存入对应的值,没有进行判断,我们如果在if判断里面直接增加了i的值,最好先把对应的字符存起来,然后这样才不容易出 ...
- [LOJ] 分块九题 1
https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...
- c++_核桃的数量
#include <iostream> using namespace std; int gcd(int x,int y){ int temp; ){ temp=x%y; x=y; y=t ...
- 实验楼Python学习记录_挑战字符串操作
自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- Dev Express中Dock panel的使用
使用DockManager,添加DockPanel. 1,DockManager位于“导航和布局”分类中. 添加一个DockManager控件到窗体中以后,即是在当前窗体类中,添加一个DockMana ...
- luogu3415 祭坛
先二分答案转化成判定问题. 考虑拿一根扫描线从 \(x=0\) 扫到 \(x=n\),每次移动扫描线更新每个位置它上面的点数和下面的点数,这样可以确定在当前的扫描线上哪些位置对于 \(y\) 轴方向是 ...
- BNU 13174 Substring Frequency
3C. Substring Frequency Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %lld ...
- python008 Python3 字符串
var1 = 'Hello World!' var2 = "QQ603374730" Python 访问字符串中的值Python 不支持单字符类型,单字符也在Python也是作为一 ...
- sql通配符+sql中查询条件包含下划线等通配符的写法
一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...