【问题标题】:How to rotate UIImage to post a photo on a website/Facebook with proper orientation?如何旋转 UIImage 以在网站/Facebook 上以正确的方向发布照片?
【发布时间】:2011-09-05 18:22:12
【问题描述】:

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

【问题讨论】:

  • here 上也有类似的帖子。
  • 有一个解决方案here,其中还包含关于实际情况的冗长解释。

标签: iphone ios facebook uiimage


【解决方案1】:

我在 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];

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多