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

时间:2011-09-05 18:22:12

标签: iphone ios facebook uiimage

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

1 个答案:

答案 0 :(得分:0)

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

希望有所帮助!