2011-09-05 57 views
2

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

+0

这里有一个类似的帖子[这里](http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload)。 – Alan

+0

有一个解决方案[这里](http://stackoverflow.com/questions/20204495/mfmailcomposeviewcontroller-image-orientation),其中还包括关于实际正在发生的冗长解释。 – Gallymon

回答

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

希望帮助!