iPhone AVFoundation 相机方向

2024-05-15

我一直在绞尽脑汁地试图让 AVFoundation 相机以正确的方向(即设备方向)捕获图片,但我无法让它工作。

我看过教程,看过 WWDC 演示,还下载了 WWDC 示例程序,但即使这样也没有成功。

我的应用程序的代码是...

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
    if (imageDataSampleBuffer != NULL)
    {
        //NSLog(@"%d", screenOrientation);

        //CMSetAttachment(imageDataSampleBuffer, kCGImagePropertyOrientation, [NSString stringWithFormat:@"%d", screenOrientation], 0);

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];

        [self processImage:image];
    }
}];

(processImage 使用与 WWDC 代码相同的 writeImage... 方法)

WWDC 应用程序的代码是...

AVCaptureConnection *videoConnection = [AVCamDemoCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
        if ([videoConnection isVideoOrientationSupported]) {
            [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
        }

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                             completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                                 if (imageDataSampleBuffer != NULL) {
                                                                     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                     UIImage *image = [[UIImage alloc] initWithData:imageData];                                                                 
                                                                     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                                                                     [library writeImageToSavedPhotosAlbum:[image CGImage]
                                                                                               orientation:(ALAssetOrientation)[image imageOrientation]
                                                                                           completionBlock:^(NSURL *assetURL, NSError *error){
                                                                                               if (error) {
                                                                                                   id delegate = [self delegate];
                                                                                                   if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                                                       [delegate captureStillImageFailedWithError:error];
                                                                                                   }                                                                                               
                                                                                               }
                                                                                           }];
                                                                     [library release];
                                                                     [image release];
                                                                 } else if (error) {
                                                                     id delegate = [self delegate];
                                                                     if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                         [delegate captureStillImageFailedWithError:error];
                                                                     }
                                                                 }
                                                             }];

在代码的开头,他们将 AVOrientation 设置为纵向,这看起来很奇怪,但我试图让它检测设备的当前方向并使用它。

正如你所看到的,我已经放置了 [UIApplication sharedApplication]statusBarOrientation 来尝试获取它,但它仍然只保存纵向照片。

任何人都可以对我需要做什么提供任何帮助或建议吗?

Thanks!

Oliver


好吧,我花了很长时间进行水力压裂,但我已经做到了!

我正在寻找的代码是

[UIDevice currentDevice].orientation;

这就是这样

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
}

而且效果很好:D

呜呜!

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

iPhone AVFoundation 相机方向 的相关文章

随机推荐

  • 如何使用 C# 以低分辨率形式提供高分辨率图像

    尝试使用 300dpi tif 图像在网络上显示 目前 当用户上传图像时 我正在动态创建缩略图 如果创建的页面引用宽度为 500x500px 的高分辨率图像 我可以使用相同的功能即时转换为 gif jpg 吗 将创建的 jpg 的即将分辨率
  • GAE 无法部署到 App Engine

    我正在尝试从 Eclipse 发布 Web 应用程序 我在 GAE 上创建了四个项目 可以通过登录我的帐户并查看控制台来查看它们 我已经改变了appengine web xml到项目的应用程序 ID 如果我将其更改为 GAE 上第一个创建的
  • 由于 http 响应 405,无法执行 mvn tomcat:deploy

    我正在将 Tomcat 7 作为 Windows 服务运行 我想在我的项目根目录中进行 mvn tomcat 部署 但总是出现这个错误 你能帮我解决这个问题吗 INFO Deploying war to http localhost 808
  • 从 URL 获取页面内容?

    我想通过以下代码从 URL 获取页面内容 public static String getContentResult URL url throws IOException InputStream in url openStream Stri
  • 奇怪的父/子NSManagedObjectContext现象

    我创建了两个这样的上下文 create writer MOC privateWriterContext NSManagedObjectContext alloc initWithConcurrencyType NSPrivateQueueC
  • asyncio.gather 中的任务不能同时工作

    我想同时从网站上抓取数据 但我发现以下程序没有同时执行 async def return soup url r requests get url r encoding utf 8 soup BeautifulSoup r text html
  • 红宝石边缘情况

    ruby 有一些边缘情况很难解释 因为解析会带来一些有趣的问题 我在这里列出其中两个 如果您知道更多 请添加到列表中 def foo 5 end this one works if tmp foo puts tmp to s end How
  • JavaFX MediaPlayer - 音乐在 10 秒后停止

    这是代码 就像标题所说的 音乐在10秒后停止 我在vlc或其他程序中正常播放该文件 持续了5分钟多 public void music String bip src data fjordmusic mp3 Media hit new Med
  • ggplot2 - 添加具有不同中断和标签的辅助 y 轴

    是否可以使用 ggplot2 手动向辅助 y 轴添加中断和标签 see bottom right 我希望在右侧 y 轴上有更紧凑的中断 代表条形 该图将作为基本情况 然后我将展示如何更改辅助 y 轴上的分隔符和标签 sapply c pip
  • 具有客户端凭据 OAuth 流的 Dynamics CRM 2016 Online Rest API

    我正在尝试使用 Dynamics CRM 2016 Online 和 Azure Active Directory 进行身份验证 我能够按照此处的所有步骤进行操作 https msdn microsoft com en us library
  • 如何在rails3中渲染编辑视图并发布Flash消息

    在我的帐户控制器中 我想在保存更改后显示 渲染 redirect to 编辑视图并显示闪存通知 def update account Account find params id respond to do format if accoun
  • 如何将多个条件应用于 db_select 上的 leftjoin?

    我正在尝试加入每个节点的五星级评级 以便我可以按评级对它们进行排序 问题是我正在查询的 Fivestar 值作为每个节点的多行存储在 Votingapi cache 中 投票数和平均评分 所以我想执行以下操作 LEFT JOIN votin
  • sizeof(某个指针)总是等于四吗?

    例如 sizeof char 返回 4 也是如此int long long 我尝试过的一切 这有什么例外吗 您得到的保证是sizeof char 1 没有其他保证 包括不保证sizeof int sizeof double 实际上 在 16
  • 目标中的 clr.dll 版本与构建的 mscordacwks.dll 版本不匹配

    我正在使用 NET Framework 4 0 开发 ASP NET MVC 3 应用程序 当我去调试我的应用程序时 我收到消息 无法附加到应用程序 webdev webserver 目标中的 clr dll 版本与构建 mcordacwk
  • Spring @ContextConfiguration

    我正在运行下一个测试 import static org junit Assert assertEquals import org junit Test import org junit runner RunWith import org
  • 在 CosmosDb 中使用 /id 作为分区键的含义

    在每分钟有 1000 个条目 唯一键 进入 cosmos 的场景中 使用 id 作为分区键安全吗 特别是 有一个逻辑分区的概念https learn microsoft com en us azure cosmos db partition
  • 我需要代码在两行之间复制并粘贴到另一张表中,并给出任何值?

    例如 我有 50 行数据 第一行有学生的名字 我需要代码将数据从 RAM 复制到 RAMESH 在这之间我有 20 行 我需要代码来复制行并将其粘贴到另一张纸中 它不应该问我名字 默认情况下 它必须采用 RAM 和 RAMESH 名称 好的
  • laravel cron 使用错误

    Laravel 错误 cron 使用 usr bin php home sitevk artisan 计划 运行 1 gt gt dev null 2 gt 1 应用 控制台 内核 use Illuminate Console Schedu
  • Javascript 原型访问另一个原型函数

    function Scroller id this ID id this obj id this currentSlide 1 var self this setInterval self nextSlide 1000 Scroller p
  • iPhone AVFoundation 相机方向

    我一直在绞尽脑汁地试图让 AVFoundation 相机以正确的方向 即设备方向 捕获图片 但我无法让它工作 我看过教程 看过 WWDC 演示 还下载了 WWDC 示例程序 但即使这样也没有成功 我的应用程序的代码是 AVCaptureCo