Image Orientation UIImagePickerController

Can someone tell me how to use the UIImagePickerController (camera and album) delegates, determine the image orientation, rotate accordingly and assign it to a UIImageView?

+3
source share
3 answers

No, UIImagePickerController cannot do this explicitly.

If you want to determine based on the contents of the image. This is a kind of difficult problem. How can you determine the orientation of an image without understanding the contents inside it?

, , . , .

-4

- , . 5 .

, , "" :

UIImagePickerController -

+14
    // Use this UIImagePickerController  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: UIImageViewreads a property imageOrientationand displays UIImageaccordingly.

+2
source

Source: https://habr.com/ru/post/1765141/


All Articles