【发布时间】:2011-09-05 18:22:12
【问题描述】:
如果我用 iPhone 相机拍照,我看到这张照片旋转了 90 度。例如,如果设备方向是 UIDeviceOrientationPortrait,则 UIImage 方向是 UIImageOrientationRight。 如何旋转 UIIMage,以便在将其发布到网站或 Facebook 时,照片的方向正确?
【问题讨论】:
标签: iphone ios facebook uiimage
如果我用 iPhone 相机拍照,我看到这张照片旋转了 90 度。例如,如果设备方向是 UIDeviceOrientationPortrait,则 UIImage 方向是 UIImageOrientationRight。 如何旋转 UIIMage,以便在将其发布到网站或 Facebook 时,照片的方向正确?
【问题讨论】:
标签: iphone ios facebook uiimage
我在 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];
希望有帮助!
【讨论】: