从相机捕获的iphone图像自动旋转-90度

2024-01-06

以编程方式,我已在应用程序中从相机中获取图像。它已经很好地获取了,但是当我切换到另一个视图并关闭该视图时,我的图像会自动旋转 -90 度。

这种变化仅在我移动之后第一次发生,当我移动时,没有发生任何变化意味着图像保持在 -90 度状态,并且仅当我从相机捕获图像时才会发生这种情况。当我从照片库获取图像时,没有发现问题。

下图是我的原图

这是旋转图像

我不知道为什么会发生这种变化。


您必须使用此功能来旋转相机捕获的图像

 - (void)imagePickerController:(UIImagePickerController *)picker
                didFinishPickingImage:(UIImage *)image
                                    editingInfo:(NSDictionary *)editingInfo
{
    image = [self scaleAndRotateImage:image];
    [self useImage:image];
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}


- (void)scaleAndRotateImage:(UIImage *)image
{
    int kMaxResolution = 320; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self setRotatedImage:imageCopy];
    //return imageCopy;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从相机捕获的iphone图像自动旋转-90度 的相关文章

随机推荐

  • Google Apps 脚本函数 getActiveSheet() 不返回活动工作表

    Google Apps 脚本开发人员指南对 活动工作表 的定义 https developers google com apps script reference spreadsheet spreadsheet app getActiveS
  • 如何使用 Jedis 连接到 AWS ElastiCache for Redis 集群?

    以前 我们使用 Redis 时通过 AWS ElastiCache 禁用了集群模式 我们使用 Jedis 的 Java 代码指向主单节点端点 用于读取和写入 我们现在已经启用了集群模式 我们现在已更改代码以指向新 Redis 集群的配置端点
  • python中的多元(多项式)最佳拟合曲线?

    如何在 python 中计算最佳拟合线 然后将其绘制在 matplotlib 中的散点图上 我使用普通最小二乘回归计算线性最佳拟合线 如下所示 from sklearn import linear model clf linear mode
  • 创建位于屏幕下方的页脚元素

    我有一个页面没有填满整个屏幕的高度 但我希望页脚位于屏幕正下方 这样当您开始滚动时它就会显示出来 无论用户的屏幕高度如何 我如何使用 CSS 来实现这一点 EDIT 我努力了 footer position absolute left 0p
  • JavaScript 中的 Require() 函数

    当我打开 Chrome 14 的控制台并输入 require or require 如果这很重要的话 I get ReferenceError 这意味着 JavaScript 默认情况下没有该功能 对吧 至少在网络浏览器上是这样 我为什么要
  • 加载从故事板实例化的 nib 文件

    所以我对这个故事板概念还很陌生 我有一个视图笔尖放入情节提要中 每个视图笔尖都对应于我拥有的 UIViewController 子类 我尝试使用以下代码加载笔尖文件 TestViewController vc TestViewControl
  • 仅在 reloadData 完成后调用函数

    我有一个tableView并且需要执行一次功能tableView已重新加载 我怎么知道是否reloadData已完成 假设我有 methodS 填充tableView 还有一次 tableView1 reloadData 已经完成了 我要调
  • Swagger - Web API - 可选查询参数

    HttpGet Route students SwaggerOperation Tags new Student SwaggerResponse HttpStatusCode OK Type typeof ResponseModel
  • 如何在 iOS 中通过滑动手势实现卷页?

    我需要在 iOS 中使用滑动手势进行卷页 我研究过Leaves项目 https github com brow leaves https github com brow leaves 但不支持滑动手势 有人成功地用滑动手势实现了卷页吗 Th
  • 如何循环jquery返回的JSON数据? [复制]

    这个问题在这里已经有答案了 可能的重复 如何在 MVC 应用程序中返回 JSON 并循环遍历 jQuery 中返回的 json https stackoverflow com questions 5953761 how do i retur
  • 如何允许在 raphael 对象中输入文本,比如矩形?

    我创建了一个拉斐尔矩形 如下所示 var rect1 paper rect 100 100 100 100 现在 我希望当我单击矩形时会出现一个光标 并且允许用户输入 键入一些文本 我对 JS 和 Raphael 非常陌生 这不是拉斐尔的自
  • Spring:在上下文根之外提供静态资源

    在网络应用程序中 我需要提供位于应用程序上下文目录之外的静态内容 图像 整个应用程序架构要求我使用 Tomcat 来执行此操作 我以为我可以从 Spring 中受益
  • Nodejs 驱动程序支持哪些聚合游标方法?

    正如您从 Mongodb 2 6 中了解到的aggregate 操作返回一个游标 但是行为有点不同 http docs mongodb org manual reference method db collection aggregate
  • 垃圾收集是否在 GC.Collect() 之后立即运行? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 这个问题只是为了研究目的 我读过很多关于 C 的书籍 这个问题总是浮现在我的脑海中 据我了解 C 是托管代码 当 CLR 决定何时运行垃圾收
  • LaTeX 中的条件导入?

    我将记下大量的课堂笔记 然后将它们编译成 LaTeX 这样我就可以拥有优秀的文档供将来查看 我正在尝试组织一些事情 以便我可以拥有一堆包含讲座笔记的小文档 然后在学期结束时将它们编译成包含所有这些笔记的大文档 我过去曾成功地使用过 impo
  • 使用 Gradle 覆盖 GCM 权限包前缀

    我有一个 Gradle Android 项目 有 4 种产品风格 每种产品都有自己独特的包名称 这build gradle文件的性质非常简单 buildscript repositories mavenCentral dependencie
  • 将 DataTable 导出到 CSV 时出现逗号问题

    我采用了一些将 DataTable 转换为 CSV 文件的代码 它似乎工作得很好 除了在实际数据中使用逗号时 在这种情况下有没有办法显示逗号 这就是我所做的 StringBuilder sb new StringBuilder IEnume
  • 了解 gc.get_referrers

    我正在尝试跟踪 Python 2 7 中的内存泄漏 我找到了 gc get referrers 但不理解输出 删除后dying node 除了我在狩猎过程中创建的列表之外 这应该删除所有引用 我的代码中有 gc collect print
  • 在 wpf 应用程序中安装窗口服务

    我有两个项目 wpf 和 windows 服务 我已经为 wpf 项目创建了设置 我想使用 wpf 项目安装来安装窗口服务 即一旦用户安装 wpf 项目 窗口服务将自动安装 是否可以 请建议 Thanks None
  • 从相机捕获的iphone图像自动旋转-90度

    以编程方式 我已在应用程序中从相机中获取图像 它已经很好地获取了 但是当我切换到另一个视图并关闭该视图时 我的图像会自动旋转 90 度 这种变化仅在我移动之后第一次发生 当我移动时 没有发生任何变化意味着图像保持在 90 度状态 并且仅当我