2011-06-15 106 views
5

CaptureStillImageAsynchronouslyFromConnection存在一個奇怪的問題。如果在鏡像視頻時使用jpegStillImageNSDataRepresentation保存圖像,則相機膠捲中的圖像將順時針旋轉90度。但是,如果它沒有鏡像,方向就很好。 我會發布代碼,任何人都有這個問題/知道修復?AVCaptureMovieFileOutput將照片保存到相機膠捲時導致錯誤的方向

更新:只是跑了一些測試,高度和寬度(640x480)都很好,反映了設備的方向。當我以縱向拍攝照片時,它會報告UIImageOrientationLeft和鏡像時的UIImageOrientationLeftMirrored。

更新2:當我在相機膠捲中查看保存的照片時,圖像的預覽具有正確的方向,當您在照片之間滑動時的圖像也是如此,但是當照片完全加載時,它會旋轉90度。這可能是相機膠捲問題嗎? (我在4.3.3)

- (void) captureImageAndSaveToCameraRoll 
{ 
    AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; 
    if ([stillImageConnection isVideoOrientationSupported]) 
     [stillImageConnection setVideoOrientation:[self orientation]]; 

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection 
                 completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

                  ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { 
                   if (error) { 
                    if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { 
                     [[self delegate] captureManager:self didFailWithError:error]; 
                    } 
                   } 
                  }; 

                  if (imageDataSampleBuffer != NULL) { 
                   NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
                   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

                   UIImage *image = [[UIImage alloc] initWithData:imageData]; 
                   [library writeImageToSavedPhotosAlbum:[image CGImage] 
                          orientation:(ALAssetOrientation)[image imageOrientation] 
                         completionBlock:completionBlock]; 
                   [image release]; 

                   [library release]; 
                  } 
                  else 
                   completionBlock(nil, error); 

                  if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) { 
                   [[self delegate] captureManagerStillImageCaptured:self]; 
                  } 
                 }]; 
} 
+0

調試這可能會有所幫助,如果你在這兩種模式下輸出image.size和imageOrientation,你會得到什麼值? – mackross 2011-06-15 01:58:50

+0

只是跑了一些測試,高度和寬度(640x480)都很好,反映了設備的方向。當我以縱向拍攝照片時,它會報告UIImageOrientationLeft和鏡像時的UIImageOrientationLeftMirrored。 – 2011-06-15 02:13:21

回答

2

我想知道,如果你新創建的圖像確實有它的方向設定爲假定你是在這裏:

[library writeImageToSavedPhotosAlbum:[image CGImage] orientation: (ALAssetOrientation) [image imageOrientation] completionBlock:completionBlock]; 

[image imageOrientation]特別似乎潛在的問題的根源,特別是需要投.. 。

你說你是什麼地方看到的形象定位,是從[image imageOrientation]只是創建後:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation: imageDataSampleBuffer]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 

我想知道你是否假設這個元數據是從AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:返回的imageData,那實際上只包含圖像像素數據本身?

+0

請注意,它[特別記錄](http://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009722-CH1- SW4)UIImageOrientation可以轉換爲ALAssetOrientation。 – Anomie 2011-06-25 18:26:07

+0

啊,很好,很棒!它獲得了正確的價值嗎?正如我上面所說,我想知道元數據是否進入'imageData',然後被解析並添加到'image'中。只是試圖追蹤數據在哪裏開始並在系統中移動,因爲它似乎沒有得到它需要的地方,如果我理解你的問題。 – Dad 2011-06-26 23:07:03

0

some reports,iPhone的照片應用早在2009年不支持任何在所有鏡像取向;他們當然可以部分解決這個問題,但在某些情況下仍然存在缺陷(我知道,例如,在某些情況下,UIImageView不能正確處理contentStretch)。這應該很容易測試:只需將該博客的示例圖像加載到相機膠捲中,然後查看會發生什麼。

+0

我使用的代碼是AVCam 1.0和1.2以及我自己的代碼的混合。我想這就是你無法在AVCam 1.2中保存照片的原因?笨。爲什麼我必須將照片保存爲鏡像?我想要做的就是保存鏡像視頻的照片。 – 2011-06-19 20:36:23

+0

在保存到相機膠捲之前,是否有一種簡單的方式拍照並垂直翻轉? – 2011-06-19 20:37:48

+1

@WDyson:當然,請參閱http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload/5427890#5427890瞭解上述問題。 – Anomie 2011-06-20 02:15:07

0

創建的UIImage的類別保存到您的圖書館前iOS UIImagePickerController result image orientation after upload

調用圖像上這種方法描述。

+0

好奇的事情。我的圖像現在全部旋轉180度,倒過來。 – 2011-06-20 20:28:17

+0

你將不得不修改這個來做你想做的事。雖然邏輯應該是相似的。 – hundreth 2011-06-20 20:48:18

+1

我冒昧地編輯你的答案,只是鏈接到我以前對圖像旋轉的回答,而不是逐字拷貝它。 -1爲剽竊,並只是從我發表評論的鏈接複製你的答案到14小時前的答案。 – Anomie 2011-06-21 18:48:46

相關問題