Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does -writeImageToSavedPhotosAlbum orient my picture incorrectly?

I am using -writeImageToSavedPhotosAlbum to sent images to the Photo Library. The problem I am having is that despite specifying an orientation, the images end up with the wrong orientations.

In fact, it seems that ones that are oriented AVCaptureVideoOrientationPortrait end up looking like AVCaptureVideoOrientationPortraitUpsideDown, and AVCaptureVideoOrientationLandscapeLeft end up looking like AVCaptureVideoOrientationLandscapeRight and vice versa.

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

I have no ViewController doing the automatic orientation change for my view.
All images show [image imageOrientation] equal to AVCaptureVideoOrientationPortrait, but I keep track of the actual orientation and pass that in to -writeImageToSavedPhotosAlbum independently.

I'm using: -writeImageToSavedPhotosAlbum:orientation:completionBlock:

I'd appreciate it if someone could shed some light onto this.

UPDATE:

Despite what I read in the documentation,

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.

I went ahead and changed the orientation I pass in:

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 images get oriented with their left edge down, as if they are landscape-right. I still don't have it right, but at least I have them uniformly oriented.

Another UPDATE:

This works. Why, I don't 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");
                          }];
like image 781
mahboudz Avatar asked Oct 31 '11 21:10

mahboudz


People also ask

How do I fix the orientation of a picture?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it. That process reconstructs the image along the requested dimensions.

Why do my photos change orientation?

Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.

Why do some images load sideways?

Photos taken on smartphones, tablets and some cameras can look great on your device but appear upside down or sideways when uploaded to a post or page because the device stores the image's orientation in the EXIF metadata and not all software is able to read the metadata.


1 Answers

You have this issue because UIDeviceOrientation and ALAssetOrientation have different enum values. Check definition for both enum 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 same will be UIDeviceOrientationUnknown (int value 0); for the UIDeviceOrientationPortrait will be equal ALAssetOrientationDown (int value 1) and so on

like image 84
Vitalii Gozhenko Avatar answered Sep 26 '22 00:09

Vitalii Gozhenko