我一直在努力让 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 设置为 Portrait,这看起来很奇怪,但我试图让它检测设备的当前方向并使用它。
如您所见,我已将 [UIApplication sharedApplication]statusBarOrientation 放入尝试获取此信息,但它仍然仅将照片保存为纵向。
任何人都可以就我需要做的事情提供任何帮助或建议吗?
谢谢!
奥利弗