Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGImage orientation issues when retrieved from ALAssset

I am just starting to learn iPhone app development. I am trying to retrieve photos from my photo album using ALAsset class and upload the photos to my server. However, the photos that were taken in the Portrait mode are rotated 90 degrees to the left while the landscape photos are properly uploaded. What am I doing wrong? In the NSLog, I do see the orientation to be 3, but still the photo is rotated to 90 degrees to the left. Any help will be greatly appreciated.

Here's the snippet of my code:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];

   imagePath = [[documentsDirectory stringByAppendingPathComponent:@"latest_photo.jpg"] copy];
   NSLog(@"imagePath = %@", imagePath);

   ALAssetRepresentation *rep = [myasset defaultRepresentation];
   ALAssetOrientation orientation = [rep orientation];
   NSLog(@"Orientation = %d", orientation);

   CGImageRef iref = [rep fullResolutionImage];

   if (iref) {
      UIImage *largeimage = [UIImage imageWithCGImage:iref scale:1.0 orientation:orientation];
      NSData *webData = UIImageJPEGRepresentation(largeimage,0.5);

      [webData writeToFile:imagePath atomically:YES];

      // Upload the image
like image 201
Balaji Krishnan Avatar asked Jan 27 '26 12:01

Balaji Krishnan


1 Answers

For the asset orientation part of your code, instead of

ALAssetRepresentation *rep = [myasset defaultRepresentation];
ALAssetOrientation orientation = [rep orientation];

Try

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
like image 89
adriaan Avatar answered Jan 30 '26 02:01

adriaan