我一直在试图让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设置为尝试获取此功能,但它仍然只能以纵向保存照片 .
任何人都可以就我需要做的事情提供任何帮助或建议吗?
谢谢!
奥利弗
11 回答
嗯,这是我永远的水力压裂,但我已经做到了!
我正在寻找的代码是
这是如此
它完美无缺:D
Woop woop!
这不是更干净吗?
以下是来自AVCam,我也补充说:
并确保将此添加到您的init方法:
有两件事需要注意
a)正如Brian King所写 - LandscapeRight和LandscapeLeft在枚举中交换 . 请参阅AVCamCaptureManager示例:
b)还有
UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
状态,如果您尝试设置为视频方向,则视频将无法录制 . 确保在调用[UIDevice currentDevice].orientation
时不使用它们!如果您正在使用AVCaptureVideoPreviewLayer,则可以在视图控制器中执行以下操作 .
(假设您有一个名为“previewLayer”的AVCaptureVideoPreviewLayer实例)
我在Swift中编写此代码,以防有人可能需要 .
第1步:生成方向通知(在
viewDidLoad
中)第二步:拍照 . 在这里,我们将互换
videoConnection
的方向 . 在AVFoundation中,方向略有变化,尤其是横向方向 . 所以我们将交换它 . 例如,我们将从LandscapeRight
更改为LandscapeLeft
,反之亦然注意:请注意横向方向的新方向值 . 恰恰相反 . (这是罪魁祸首:: UHHHH)
第3步:修复方向(UIImage扩展)
这使用视图控制器的方向方法 . 这对我有用,希望对你有用 .
在Swift中你应该这样做:
它对我来说很完美!
您还可以创建中间CIImage,并获取属性字典
并相应变换:)
我喜欢在iOS5中访问所有这些图像元数据是多么容易!
启动捕获会话后以及旋转设备时更新预览图层中的方向 .
如何使用AVCaptureFileOutput?