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

mah*_*udz 9 avfoundation ios

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

事实上,似乎那些面向AVCaptureVideoOrientationPortrait最终看起来像AVCaptureVideoOrientationPortraitUpsideDown,并AVCaptureVideoOrientationLandscapeLeft最终看起来像AVCaptureVideoOrientationLandscapeRight,反之亦然.

这有什么原因吗?以下是一些更多细节:

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

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

如果有人能对此有所了解,我会很感激.

更新:

尽管我在文档中读到了

如果要保存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");
                      }];
Run Code Online (Sandbox Code Playgroud)

现在,所有图像都以其左边缘向下定向,就像它们是风景右侧一样.我仍然没有正确,但至少我让他们统一导向.

另一个更新:

这有效.为什么,我不知道:

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");
                          }];
Run Code Online (Sandbox Code Playgroud)

Vit*_*nko 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
};
Run Code Online (Sandbox Code Playgroud)

因此对于 UIDeviceOrientationUnknown 来说,同样是 UIDeviceOrientationUnknown (int value 0); 对于 UIDeviceOrientationPortrait 将等于 ALAssetOrientationDown (int value 1) 等等