gpt4 book ai didi

ios - 为什么 -writeImageToSavedPhotosAlbum 不正确地定位我的图片?

转载 作者:可可西里 更新时间:2023-11-01 03:00:02 24 4
gpt4 key购买 nike

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

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

发生这种情况有什么原因吗?以下是更多详细信息:

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

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

如果有人能阐明这一点,我将不胜感激。

更新:

尽管我在文档中看到了什么,

If you want to save a UIImage object, you can use the UIImage method CGImage to get a CGImageRef, and cast the image’s imageOrientation to 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");
}];

最佳答案

您遇到此问题是因为 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 值 1)等等

关于ios - 为什么 -writeImageToSavedPhotosAlbum 不正确地定位我的图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com