CGImage orientation problems when retrieving from ALAssset

I'm just starting to learn iPhone app development. I am trying to extract photos from my photo album using the ALAsset class and upload the photos to my server. However, photographs taken in portrait mode are rotated 90 degrees to the left, and landscape photographs load correctly. What am I doing wrong? In NSLog, I see that the orientation is 3, but still the photo rotates 90 degrees to the left. Any help would be greatly appreciated.

Here is a 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
+3
source share
2 answers

For the part of determining the orientation of your code instead

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

Try

ALAssetOrientation orientation = [[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
+8
source

. .

UIGraphicsBeginImageContext(largeImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI / 2); // Or negative M_PI depending on the image orientation
largeImage.imageOrientation = UIImageOrientationLeft;
[largeImage drawAtPoint:CGPointZero];
NSData *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

"" , , , .

0

All Articles