简体   繁体   中英

How to rotate UIImage to post a photo on a website/Facebook with proper orientation?

if I take a photo with the iPhone camera, I see that photo is 90 degree rotated. For instance if device orientation is UIDeviceOrientationPortrait the UIImage orientation is UIImageOrientationRight. How can I rotate the UIIMage so that if I post it on a website or Facebook, the photo is correctly orientated?

I'm using this function in a UIImage category:

- (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode
                              bounds:(CGSize)bounds
                interpolationQuality:(CGInterpolationQuality)quality {
    CGFloat horizontalRatio = bounds.width / self.size.width;
    CGFloat verticalRatio = bounds.height / self.size.height;
    CGFloat ratio;

    switch (contentMode) {
        case UIViewContentModeScaleAspectFill:
            ratio = MAX(horizontalRatio, verticalRatio);
            break;

        case UIViewContentModeScaleAspectFit:
            ratio = MIN(horizontalRatio, verticalRatio);
            break;

        default:
            [NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode];
    }

    CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);

    return [self resizedImage:newSize interpolationQuality:quality];
}

then just call:

UIImage *myCameraRollImage = ..//image received from camera roll

UIImage *mFixedRotationImage = [myCameraRollImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(width, height) interpolationQuality:kCGInterpolationHigh];

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM