【问题标题】:Rotating portrait UIImage that somehow has a UIImageOrientationRight orientation以某种方式具有 UIImageOrientationRight 方向的旋转肖像 UIImage
【发布时间】:2012-12-19 23:05:31
【问题描述】:

大家好,

我在旋转某个方向等于UIImageOrientationRight 的肖像UIImage(即WIDTH < HEIGHT)时遇到问题。我不确定这是怎么回事,但它发生在我的库中用 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,则此脚本不起作用
  • 这个问题的解决方案here 还包括对实际情况的冗长解释。

标签: iphone objective-c rotation uiimage uiimageorientation


【解决方案1】:

首先,请原谅我的英语,因为它不是我的母语。我希望现在给出这个答案还不算晚!

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

因此,要从使用 iPhone 相机拍摄的图库中加载照片,您必须这样做:

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

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

嗯,我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 2013-11-25
    • 2019-12-15
    • 1970-01-01
    • 2015-09-12
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多