UIImagePickerController Image Orientation

2019-01-24 21:16发布

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?

3条回答
The star\"
2楼-- · 2019-01-24 21:22

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

查看更多
太酷不给撩
3楼-- · 2019-01-24 21:30
    // 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.

查看更多
再贱就再见
4楼-- · 2019-01-24 21:41

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

查看更多
登录 后发表回答