Reputation: 17581
Can anyone tell me how to use the UIImagePickerController (camera and album) delegates determine the image's orientation, rotate accordingly, and assign it to a UIImageView?
Upvotes: 3
Views: 12873
Reputation: 4733
// Use this UIImagePickerController's delegate method
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// Getting image user just has shoot
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Fixing to stick with only one orientation (UIImageOrientationUp in this case)
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
image = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:UIImageOrientationUp]; // change this if you need another orientation
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
// The image is already in correct orientation
break;
}
// Do whatever you want with image (likely pass it to some UIImageView to display)
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
// Dismiss UIImagePickerController
[picker dismissModalViewControllerAnimated:NO];
}
Note: UIImageView
reads the imageOrientation
property and displays UIImage
accordingly.
Upvotes: 2
Reputation: 33126
Vodkhang's answer is partially incorrect - modern cameras put an explicit orientation into the image when they take the photo. This has been common for about 5+ years now.
I've previously used the code in this answer to do the rotation by reading the "orientation" info direct from the picture:
UIImagePickerController camera preview is portrait in landscape app
Upvotes: 14
Reputation: 18741
No, UIImagePickerController cannot do it explicitly.
If you want to determine based on image's content. It is kind of hard problem. How can you determine an image's orientation without understanding the content inside of it?
I can suggest you a trick that you examine the width and the length of the returned image and see if it is portrait or landscape. For rotating the image, there are some libraries and codes outside but I didn't try them much. You can take a look at this blog
Upvotes: -4