如何从ALAssetRepresentation获得正确旋转的UIImage? [英] How to get a correctly rotated UIImage from an ALAssetRepresentation?
问题描述
我正在尝试使用从
方法。我有几张以不同设备方向拍摄的测试照片;照片在照片应用中正确显示。 ALAssetRepresentation
获得正确旋转的 UIImage
fullScreenImage fullScreenImage
的文档说:
I’m trying to get a correctly rotated UIImage
from an ALAssetRepresentation
using the fullScreenImage
method. I have several testing photos shot in various device orientations; the photos show up correctly in the Photos app. The documentation for fullScreenImage
says:
在iOS 5及更高版本中,此方法返回完全裁剪,旋转和
调整后的图像 - 完全按照用户在照片或图片中看到的
选择器。
In iOS 5 and later, this method returns a fully cropped, rotated, and adjusted image—exactly as a user would see in Photos or in the image picker.
创建一个正确旋转 UIImage
来自 CGImage
的对象,你使用
imageWithCGImage:scale :orientation:
或
initWithCGImage:scale:orientation:
,传递 orientation的值
和 scale
。
To create a correctly-rotated UIImage
object from the CGImage
, you use
imageWithCGImage:scale:orientation:
or
initWithCGImage:scale:orientation:
, passing the values of orientation
and scale
.
鉴于文档,我的代码如下所示:
Given the docs, my code looks like this:
ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
imageWithCGImage:[rep fullScreenImage]
scale:[rep scale]
orientation:[rep orientation]];
但结果 UIImage
的轮换是错误。当我用 UIImageOrientationUp
替换 [rep orientation]
时,图像适用于所有测试照片。显然我对于坚持这个解决方案犹豫不决,因为它感觉像是一个黑客。我做错了什么?
But the resulting UIImage
’s rotation is wrong. When I replace [rep orientation]
with UIImageOrientationUp
, the image is fine for all testing photos. Obviously I’m hesitating to stick with this "solution", as it feels like a hack. What am I doing wrong?
推荐答案
ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
imageWithCGImage:[rep fullScreenImage]
scale:[rep scale]
orientation:UIImageOrientationUp];
在iOS 5下是正确的,全屏图像已经旋转(所以它总是向上)。在iOS 4下,行为是不同的。请参阅使用Photo中的方向行为不正确ALAsset 有更深入的解释。
Is correct as under iOS 5 the fullscreenimages is already rotated (so it’s always "up"). Under iOS 4 the behaviour is different. Please see Orientation does not behave correctly with Photo in ALAsset for a more in depth explanation.
这篇关于如何从ALAssetRepresentation获得正确旋转的UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!