Image tapped from iPhone in portrait mode rotates 90 degrees

I upload an image clicked from iphone in both landscape and portrait modes. The image with landscape mode loads fine, but the problem is with loading the image in portrait mode. They rotate 90 degrees.

In addition, other images with portrait mode (without clicking on the iPhone) work fine.

Any idea why this is happening?

+6
iphone image
May 12 '11 at 3:49 am
source share
3 answers

In your delegate:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

After you get the UIImage from the "info" dictionary for the " UIImagePickerControllerOriginalImage " key, you can see the image orientation by the imageOrientation property. If this is not the case, you just want to rotate the image before loading it.

 imageOrientation The orientation of the receiver's image. (read-only) @property(nonatomic, readonly) UIImageOrientation imageOrientation Discussion Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the "up" orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see "UIImageOrientation." Availability Available in iOS 2.0 and later. Declared In UIImage.h 

UIImage class reference

UIImagePickerController Class Reference

Link to UIImagePickerControllerDelegate Protocol

Second option:

Allows the user to edit the image and retrieve the image for " UIImagePickerControllerEditedImage ";

Set your UIImagePickerController " allowsEditing property to Yes .

As your delegate, simply enter the "info" UIImage for key <<24>.

Good luck.

+5
May 12 '11 at 7:09
source share
— -

I struggled quite a bit with this problem, I was working on a project where I need to rotate the image, such as reordering pixels so that I can load it.

The first thing you need to do is determine the orientation, then delete this annoying metadata, then rotate the image.

So put this inside the didFinishPickingMediaWithInfo function:

  UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) { //Rotate based on orientation switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) { case 3: //Rotate image to the left twice. img = [UIImage imageWithCGImage:[img CGImage]]; //Strip off that pesky meta data! img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft]; break; case 6: img = [UIImage imageWithCGImage:[img CGImage]]; img = [rotateImage rotateImage:img withRotationType:rotateRight]; break; case 8: img = [UIImage imageWithCGImage:[img CGImage]]; img = [rotateImage rotateImage:img withRotationType:rotateLeft]; break; default: break; } } 

And here is the resize function:

 +(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{ CGImageRef imageRef = [image CGImage]; CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); if (alphaInfo == kCGImageAlphaNone) alphaInfo = kCGImageAlphaNoneSkipLast; CGContextRef bitmap; bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo); CGColorSpaceRelease(colorSpaceInfo); if (rotation == rotateLeft) { CGContextTranslateCTM (bitmap, image.size.height, 0); CGContextRotateCTM (bitmap, radians(90)); } else{ CGContextTranslateCTM (bitmap, 0, image.size.width); CGContextRotateCTM (bitmap, radians(-90)); } CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage *result = [UIImage imageWithCGImage:ref]; CGImageRelease(ref); CGContextRelease(bitmap); return result; } 

Now the img variable contains a correctly rotated image.

+4
Mar 16 2018-12-12T00:
source share

Ok, cleaner version:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage]; img = [UIImage imageWithCGImage:[img CGImage]]; UIImageOrientation requiredOrientation = UIImageOrientationUp; switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) { case 3: requiredOrientation = UIImageOrientationDown; break; case 6: requiredOrientation = UIImageOrientationRight; break; case 8: requiredOrientation = UIImageOrientationLeft; break; default: break; } UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation]; } 
0
Nov 07 '16 at 8:56
source share



All Articles