Why does -writeImageToSavedPhotosAlbum orient my picture incorrectly?

I use -writeImageToSavedPhotosAlbum to send images to the Photo Library. The problem I am facing is that, despite indicating the orientation, the images end up with incorrect orientations.

Actually, those that are oriented by AVCaptureVideoOrientationPortrait look like AVCaptureVideoOrientationPortraitUpsideDown , and AVCaptureVideoOrientationLandscapeLeft look like AVCaptureVideoOrientationLandscapeRight and vice versa.

Is there a reason why this is happening? Here are some more details:

I do not have a ViewController that does an automatic orientation change for my view.
All images show [image imageOrientation] equal to AVCaptureVideoOrientationPortrait , but I keep track of the actual orientation and pass this to -writeImageToSavedPhotosAlbum independently.

I use: -writeImageToSavedPhotosAlbum:orientation:completionBlock:

I would appreciate it if someone could shed light on this.

UPDATE:

Despite the fact that I read in the documentation,

If you want to save the UIImage object, you can use the UIImage CGImage method to get the CGImageRef, and discard imageOrientation images to ALAssetOrientation.

I went ahead and changed the orientation in which I walked:

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

Now all the images are oriented with the left edge down, as if they were landscape. I still do not have the right, but at least for me they are uniformly oriented.

Other UPDATE:

It works. Why i do not know:

 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"); }]; 
+8
ios avfoundation
source share
2 answers

You have this problem because UIDeviceOrientation and ALAssetOrientation have different enumeration values. Check the definition for both listings below:

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

So, for UIDeviceOrientationUnknown there will be UIDeviceOrientationUnknown (int value 0); for UIDeviceOrientationPortrait will be equal to ALAssetOrientationDown (int value 1), etc.

+1
source share

Are you sure that the image is not saved with the correct orientation, and you simply do not respect the orientation when displaying it?

You can load the asset in the completion block and check there for correctness.

0
source share

All Articles