繁体   English   中英

如何旋转UIImage以正确的方向在网站/ Facebook上发布照片?

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

如果我使用iPhone相机拍摄照片,则会看到该照片旋转了90度。 例如,如果设备方向为UIDeviceOrientationPortrait,则UIImage方向为UIImageOrientationRight。 如何旋转UIIMage,以便将其发布在网站或Facebook上时,照片的方向是否正确?

我在UIImage类别中使用此功能:

- (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];
}

然后只需致电:

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

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

希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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