我一直在撕裂我的头发,试图让AVFoundation相机以正确的方向(即设备方向)捕捉图片,但我无法使它工作。iPhone 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来使用相同的writeImage ...方法作为WWDC代码)
和代码从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尝试并获得这个,但它仍然只能保存肖像照片。
任何人都可以提供任何帮助或建议,我需要做什么?
谢谢!
Oliver
好吧,我以为我已经修复它,但可惜我错了。它修复了在手机上显示图像并正确保存到库的问题,但是当上传到Facebook或在我的电脑上查看时,它们仍然是错误的方式:( – Fogmeister 2010-09-08 12:57:21
你最终的解决方案是什么?我注意到你的文章UIDeviceOrientation和AVCaptureVideoOrientation不完全相同,LandscapeRight和LandscapeLeft在枚举中交换,我做了很长时间(如果纵向/纵向,如果横向离开/设置横向离开),我的风景图片总是颠倒过来!似乎交换是必需的,以保持映射正确,不知道如何工作。 – 2011-01-08 17:40:33
嗯。对我而言,照片库中的缩略图方向会旋转,但图像本身很好。 – 2011-08-09 09:42:52