4

我正在尝试使用该方法正确旋转UIImage。我有几张以各种设备方向拍摄的测试照片;照片在照片应用程序中正确显示。的文档说:ALAssetRepresentationfullScreenImagefullScreenImage

在 iOS 5 及更高版本中,此方法返回完全裁剪、旋转和调整的图像——与用户在照片或图像选择器中看到的完全一样。

要从 中创建正确旋转的UIImage对象CGImage,请使用 imageWithCGImage:scale:orientation:or initWithCGImage:scale:orientation:,并传递orientation and的值scale

给定文档,我的代码如下所示:

ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
    imageWithCGImage:[rep fullScreenImage]
    scale:[rep scale]
    orientation:[rep orientation]];

但由此产生UIImage的 ' 旋转是错误的。当我替换[rep orientation]为 时UIImageOrientationUp,该图像适用于所有测试照片。显然,我在犹豫是否坚持使用这个“解决方案”,因为它感觉就像是一个 hack。我究竟做错了什么?

4

1 回答 1

11
ALAssetRepresentation *rep = [asset defaultRepresentation];
UIImage *img = [UIImage
    imageWithCGImage:[rep fullScreenImage]
    scale:[rep scale]
    orientation:UIImageOrientationUp];

是正确的,因为在 iOS 5 下全屏图像已经旋转(所以它总是“向上”)。在 iOS 4 下,行为是不同的。请参阅ALAsset中照片的方向不正确,以获得更深入的解释。

于 2012-06-06T08:07:07.207 回答