2011-10-31 21 views
8

Tôi đang sử dụng -writeImageToSavedPhotosAlbum để gửi hình ảnh đến Thư viện ảnh. Vấn đề tôi gặp phải là mặc dù chỉ định định hướng, hình ảnh kết thúc với định hướng sai.Tại sao -writeImageToSavedPhotosAlbum định hướng hình ảnh của tôi không chính xác?

Trong thực tế, có vẻ như những người được định hướng AVCaptureVideoOrientationPortrait cuối lên trông như AVCaptureVideoOrientationPortraitUpsideDown, và AVCaptureVideoOrientationLandscapeLeft cuối lên trông như AVCaptureVideoOrientationLandscapeRight và ngược lại.

Có lý do nào khiến điều này xảy ra không? Dưới đây là một số chi tiết khác:

Tôi không có ViewController thực hiện thay đổi định hướng tự động cho chế độ xem của tôi.
Tất cả hình ảnh hiển thị [image imageOrientation] bằng AVCaptureVideoOrientationPortrait, nhưng tôi theo dõi định hướng thực tế và chuyển giá trị đó đến -writeImageToSavedPhotosAlbum một cách độc lập.

Tôi đang sử dụng: -writeImageToSavedPhotosAlbum:orientation:completionBlock:

Tôi đánh giá cao nó nếu ai đó có thể làm sáng tỏ vào này.

UPDATE:

Mặc dù những gì tôi đọc trong tài liệu,

Nếu bạn muốn lưu một đối tượng UIImage, bạn có thể sử dụng phương pháp UIImage CGImage để có được một CGImageRef, và đúc hình ảnh của imageOrientation to ALAssetOrientation.

tôi đã đi trước và thay đổi định hướng tôi vượt qua trong:

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"); 
         }]; 

Bây giờ, tất cả hình ảnh được định hướng với cạnh trái của họ xuống, như thể chúng là cảnh quan bên phải. Tôi vẫn không có nó đúng, nhưng ít nhất tôi có chúng theo định hướng thống nhất.

Một lần cập nhật khác:

Tác phẩm này hoạt động. Tại sao, tôi không biết:

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" trong đoạn mã cuối cùng là gì? – ebi

Trả lời

1

Bạn có vấn đề này vì UIDeviceOrientation và ALAssetOrientation có các giá trị enum khác nhau. Kiểm tra định nghĩa cho cả hai enum dưới đây:

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 
}; 

Vì vậy, đối với UIDeviceOrientationKhông biết tương tự sẽ là UIDeviceĐối tượngKhông biết (int value 0); cho UIDeviceOrientationPortrait sẽ bằng ALAssetOrientationDown (int value 1) và như vậy trên

0

Bạn có chắc chắn hình ảnh không được lưu theo đúng hướng và bạn không tôn trọng hướng khi bạn hiển thị không?

Bạn có thể tải nội dung trong khối hoàn thành và kiểm tra hướng ở đó để chắc chắn.

Các vấn đề liên quan