image clicked from iPhone in Portrait mode gets rotated by 90 degree

后端 未结 3 1137
逝去的感伤
逝去的感伤 2020-12-09 07:19

I am uploading an image clicked from iphone both in landscape and portrait mode. The image with landscape mode is uploaded fine but the issue is with the image uploaded with

相关标签:
3条回答
  • 2020-12-09 07:54

    In your delegate:

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

    After you get your UIImage from "info" dictionary for the key "UIImagePickerControllerOriginalImage" ,You can see the image orientation by imageOrientation property. If is not like you want just rotate your image before you upload 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

    UIImagePickerControllerDelegate Protocol Reference

    Second option:

    Is to allow user to edit your image and get the image for "UIImagePickerControllerEditedImage";

    Set your UIImagePickerController "allowsEditing" property to Yes.

    In your delegate just get from the "info" dictionary the UIImage for the "UIImagePickerControllerEditedImage" key.

    Good luck.

    0 讨论(0)
  • 2020-12-09 07:56

    I've wrestled with this problem quite a bit, I was working on a project where I need to actually rotate the image, as in re-arrange the pixels so that I could upload it.

    First thing you need to do is determine the orientation, then strip off that pesky meta data, then rotate the image.

    So put this inside of 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;
    }
    

    The img variable now contains a properly rotated image.

    0 讨论(0)
  • 2020-12-09 08:01

    Ok, a cleaner version would be :

    - (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 讨论(0)
提交回复
热议问题