我一直在撕裂我的頭髮,試圖讓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