旋转肖像UIImage,其UIImageOrientationRight方向。

3
大家好,
我遇到一个问题,需要旋转一张纵向的UIImage(即WIDTH < HEIGHT),但它的方向等于UIImageOrientationRight。我不确定这是怎么发生的,但在我的库中有一些使用iPhone 4拍摄的图像会出现这种情况。
这是我的代码(只有当方向等于UIImageOrientationUp时才有效):
UIGraphicsBeginImageContextWithOptions(rotatedScaledRect.size, NO, 0.0);
CGContextRef myContext = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(myContext, kCGInterpolationHigh);

CGContextTranslateCTM(myContext,
                      (rotatedScaledRect.size.width/2),
                      (rotatedScaledRect.size.height/2));
CGContextConcatCTM(myContext, transformation);

CGContextScaleCTM(myContext, 1.0, -1.0);

CGContextDrawImage(myContext, CGRectMake(-roundf(newImage.size.width / 2), -roundf(newImage.size.height / 2), newImage.size.width, newImage.size.height), [newImage CGImage]);

newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

我需要应用CGContextConcatCTM,因为我有多个连接到图像的变换。

我尝试了几种不同的方法,但都无济于事。

非常感谢您的帮助。


我有同样的问题,如果图像方向不等于0,则此脚本无法工作。 - Pion
这个问题的解决方案在这里,其中还包括了详细的解释。 - Gallymon
1个回答

0

首先,因为英语不是我的母语,所以请原谅我的语言表达。希望现在回答还不算太晚!

我曾经遇到过从使用 iPhone 或 iPod 相机拍摄的照片库中加载照片的类似问题。如果您也遇到了这个问题,可以在这里找到一些信息:https://discussions.apple.com/thread/2495567?start=30&tstart=0,特别是其中一个回答指出:“苹果选择使用 EXIF 数据中的方向标志来通知显示图像的程序如何旋转图像,以便正确呈现图像。”

因此,要从使用 iPhone 相机拍摄的照片库中加载照片,您需要执行以下操作:

ALAssetRepresentation *assetRep = [photo defaultRepresentation];
CGImageRef imageRef = [assetRep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[assetRep scale] orientation:[assetRep orientation]];

其中photo是从相册中获取的ALAsset对象。

希望这有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接