如何旋转UIImage以正确的方向在网站/ Facebook上发布照片?
问题描述:
如果我用iPhone相机拍照,我会看到照片旋转90度。例如,如果设备方向为UIDeviceOrientationPortrait,则UIImage方向为UIImageOrientationRight。 如何旋转UIIMage,以便我将它发布到网站或Facebook上时,照片是否正确定向?如何旋转UIImage以正确的方向在网站/ Facebook上发布照片?
答
我使用这个功能在一个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];
希望帮助!
这里有一个类似的帖子[这里](http://*.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload)。 – Alan
有一个解决方案[这里](http://*.com/questions/20204495/mfmailcomposeviewcontroller-image-orientation),其中还包括关于实际正在发生的冗长解释。 – Gallymon