iPhone AVFoundation 相机方向 [英] iPhone AVFoundation camera orientation

查看:24
本文介绍了iPhone AVFoundation 相机方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让 AVFoundation 相机以正确的方向(即设备方向)捕捉图片,但我无法让它工作.

I've been tearing my hair out trying to get the AVFoundation camera to capture a picture in the correct orientation (i.e. the device orientation) but I can't get it to work.

我看过教程,我看过 WWDC 演示文稿,我已经下载了 WWDC 示例程序,但即使这样也没有用.

I have looked at tutorials, I've watched the WWDC presentation and I've downloaded the WWDC sample program but even that doesn't do it.

我应用的代码是...

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... 方法)

(processImage uses the same writeImage... method as the WWDC code)

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

and the code from the WWDC app is...

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 设置为纵向,这看起来很奇怪,但我试图让它检测设备的当前方向并使用它.

At the beginning of their code they set the AVOrientation to portrait which seems very odd but I'm trying to get it to detect the device's current orientation and use that.

如您所见,我已将 [UIApplication sharedApplication]statusBarOrientation 用于尝试获取此信息,但它仍然只保存纵向照片.

As you can see I have put [UIApplication sharedApplication]statusBarOrientation to try and get this but it still only save the photos in portrait.

任何人都可以就我需要做的事情提供任何帮助或建议吗?

Can anyone offer any help or advice on what I need to be doing?

谢谢!

奥利弗

推荐答案

好吧,这让我永远水力压裂,但我做到了!

Well, it's taken me fracking forever but I've done it!

我正在寻找的代码是

[UIDevice currentDevice].orientation;

这样就可以了

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

而且效果很好:D

呜呜呜!

这篇关于iPhone AVFoundation 相机方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆