为什么-writeImageToSavedPhotosAlbum错误地定位我的图片?

时间:2011-10-31 21:52:08

标签: ios avfoundation

我正在使用-writeImageToSavedPhotosAlbum将图片发送到照片库。我遇到的问题是,尽管指定了方向,但图像最终会出现错误的方向。

事实上,面向AVCaptureVideoOrientationPortrait的人似乎最终看起来像AVCaptureVideoOrientationPortraitUpsideDown, 并且AVCaptureVideoOrientationLandscapeLeft最终看起来像AVCaptureVideoOrientationLandscapeRight,反之亦然。

为什么会出现这种情况?以下是一些更多细节:

我没有ViewController为我的视图进行自动方向更改 所有图片都显示[image imageOrientation]等于AVCaptureVideoOrientationPortrait,但我会跟踪实际方向,并将其单独传递给-writeImageToSavedPhotosAlbum

我正在使用:-writeImageToSavedPhotosAlbum:orientation:completionBlock:

如果有人可以对此有所了解,我会很感激。

更新:

尽管我在文档中读到了,

  

如果要保存UIImage对象,可以使用UIImage方法   获取CGImageRef的CGImage,并将图像的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");
                          }];

2 个答案:

答案 0 :(得分: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 value 0); 对于UIDeviceOrientationPortrait将等于ALAssetOrientationDown(int value 1)等等

答案 1 :(得分:0)

您确定图像没有以正确的方向保存吗?您只是在显示时不尊重方向?

您可以在完成程序段中加载资产并检查其中的方向以确定。