开发者

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 webs开发者_JAVA百科ite 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜