2011-09-05 70 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]; 

希望幫助!