2011-10-31 62 views
8

我使用-writeImageToSavedPhotosAlbum將圖像發送到照片庫。我遇到的問題是,儘管指定了方向,圖像最終的方向卻是錯誤的。爲什麼-writeImageToSavedPhotosAlbum不正確地定位我的照片?

事實上,似乎是定向AVCaptureVideoOrientationPortrait末看起來像AVCaptureVideoOrientationPortraitUpsideDown, 和AVCaptureVideoOrientationLandscapeLeft結束看起來像AVCaptureVideoOrientationLandscapeRight,反之亦然的。

爲什麼會有這種情況發生?這裏有一些更多的細節:

我沒有ViewController做我的看法的自動方向更改。
所有圖片顯示[image imageOrientation]等於AVCaptureVideoOrientationPortrait,但我跟蹤實際方向並將其單獨傳遞給-writeImageToSavedPhotosAlbum

我使用:-writeImageToSavedPhotosAlbum:orientation:completionBlock:

我會很感激,如果有人可以提供一些線索到此。

UPDATE:

儘管我的文檔中讀取,

如果你想保存一個UIImage對象,你可以使用UIImage的方法 CGImage獲得CGImageRef,投圖像的imageOrientation爲 ALAssetOrientation。

我繼續改變了我在傳遞方向:

switch (lastOrientation) { 
    case UIDeviceOrientationPortrait: 
    default: 
     alOrientation = ALAssetOrientationUp; 
     break; 
    case UIDeviceOrientationPortraitUpsideDown: 
     alOrientation = ALAssetOrientationDown; 
     break; 
    case UIDeviceOrientationLandscapeLeft: 
     alOrientation = ALAssetOrientationLeft; 
     break; 
    case UIDeviceOrientationLandscapeRight: 
     alOrientation = ALAssetOrientationRight; 
     break; 

} 
[library writeImageToSavedPhotosAlbum:[image CGImage] 
          orientation:(ALAssetOrientation) alOrientation// [image imageOrientation] 
         completionBlock:^(NSURL *assetURL, NSError *error) { 
          NSLog(@"completion block"); 
         }]; 

現在,所有的圖像得到取向爲它們的左邊緣下來,彷彿他們是景觀權。我仍然沒有對,但至少我有統一導向。

另一個更新:

這個工程。爲什麼,我不知道:

switch (lockedOrientation) { 
     case UIDeviceOrientationPortrait: 
     default: 
      alOrientation = ALAssetOrientationRight; //3 instead ofALAssetOrientationUp; 
      break; 
     case UIDeviceOrientationPortraitUpsideDown: 
      alOrientation = ALAssetOrientationLeft; //2 insted of ALAssetOrientationDown; 
      break; 
     case UIDeviceOrientationLandscapeLeft: 
      alOrientation = ALAssetOrientationUp; //0 instead of ALAssetOrientationLeft; 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      alOrientation = ALAssetOrientationDown; //1 instead of ALAssetOrientationRight; 
      break; 

    } 
    [library writeImageToSavedPhotosAlbum:[image CGImage] 
           orientation:(ALAssetOrientation) alOrientation// [image imageOrientation] 
          completionBlock:^(NSURL *assetURL, NSError *error) { 
           NSLog(@"completion block"); 
          }]; 
+0

什麼是最後一段代碼中的「lockedOrientation」? – ebi

回答

1

您有此問題,因爲UIDeviceOrientation和ALAssetOrientation具有不同的枚舉值。檢查定義以下兩個枚舉:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait,   // Device oriented vertically, home button on the bottom 
    UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 
    UIDeviceOrientationLandscapeLeft,  // Device oriented horizontally, home button on the right 
    UIDeviceOrientationLandscapeRight,  // Device oriented horizontally, home button on the left 
    UIDeviceOrientationFaceUp,    // Device oriented flat, face up 
    UIDeviceOrientationFaceDown    // Device oriented flat, face down 
}; 

typedef NS_ENUM(NSInteger, ALAssetOrientation) { 
    ALAssetOrientationUp,   // default orientation 
    ALAssetOrientationDown,   // 180 deg rotation 
    ALAssetOrientationLeft,   // 90 deg CCW 
    ALAssetOrientationRight,   // 90 deg CW 
    ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 
    ALAssetOrientationDownMirrored, // horizontal flip 
    ALAssetOrientationLeftMirrored, // vertical flip 
    ALAssetOrientationRightMirrored, // vertical flip 
}; 

所以對於UIDeviceOrientationUnknown同樣會UIDeviceOrientationUnknown(int值0);對於UIDeviceOrientationPortrait, 將等於ALAssetOrientationDown(int值1)等等

0

您確定圖像沒有以正確的方向保存,並且您在顯示時只是不尊重方向?

您可以在完成塊中加載資產並檢查確定的方向。

相關問題