为什么-writeImageToSavedPhotosAlbum不正确地定位我的照片?
我使用-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");
}];
您有此问题,因为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)等等
您确定图像没有以正确的方向保存,并且您在显示时只是不尊重方向?
您可以在完成块中加载资产并检查确定的方向。
什么是最后一段代码中的“lockedOrientation”? – ebi