3

I'm having problems orienting images properly on the iPhone. I'm accessing the images using the AssetLibrary classes.

All, I want to do is properly orient the images for display, unfortunately for some images that are taken in portrait mode, they are in an orientation that doesn't make sense.

In the block that's called by the framework, I'm making the following calls for a picture that was taken by the iPhone camera in while it was oriented 'Up':

        ALAssetRepresentation   *representation = [myasset defaultRepresentation];
        ALAssetOrientation      orient          = [representation orientation];
        ALAssetOrientation      alorient = [[myasset valueForProperty:@"ALAssetOrientation"] intValue];
        UIImage                 *timg           = [[UIImage alloc] initWithCGImage:[representation fullScreenImage]];
        UIImageOrientation      imgorient       = [timg imageOrientation];

THe variable orient = ALAssetOrientationRight.
The variable alorient = ALAssetOrientationUp.
The variable imgorient = UIImageOrientationUp

I was using the orient value to rotate the image so that it appears 'up'. Unfortunately, if I have an image that's taken in landscape mode the same code doesn't work because the orientations are incorrect.

I've tried instantiating timg using the scale:orientation: overload using orient for the orientation parameter, all it does is initialize the image setting the orientation but incorrectly rotates the image.

This seems non deterministic, how come these calls return different results:

ALAssetOrientation      orient   = [representation orientation];
ALAssetOrientation      alorient = [[myasset valueForProperty:@"ALAssetOrientation"] intValue];

Your help will be greatly appreciated.

Thanks, Will

willmapp
  • 61
  • 1
  • 3

1 Answers1

0

The problem is that you used the wrong property name.

In your code:

[[myasset valueForProperty:@"ALAssetOrientation"] intValue];

should be:

[[myasset valueForProperty:@"ALAssetPropertyOrientation"] intValue];

According to the documentation of -[ALAsset valueForProperty:]

If property is not a valid key, returns ALErrorInvalidProperty.

Your code try to cast ALErrorInvalidProperty to an int, which result in 0. By coincidence ALAssetOrientationUp have the value of 0 that's why your alorient variable will always evaluate to ALAssetOrientationUp.

howanghk
  • 3,070
  • 2
  • 21
  • 34