【问题标题】:How to get a correctly rotated UIImage from an ALAssetRepresentation?如何从 ALAssetRepresentation 中获取正确旋转的 UIImage?
【发布时间】:2012-06-08 10:31:30
【问题描述】:

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

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

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

根据文档,我的代码如下所示:

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

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

【问题讨论】:

    标签: objective-c ios alassetslibrary alasset


    【解决方案1】:
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    UIImage *img = [UIImage
        imageWithCGImage:[rep fullScreenImage]
        scale:[rep scale]
        orientation:UIImageOrientationUp];
    

    在 iOS 5 下是正确的,全屏图像已经旋转(所以它总是“向上”)。在 iOS 4 下,行为有所不同。请参阅Orientation does not behave correctly with Photo in ALAsset 以获得更深入的解释。

    【讨论】:

    • 您好,Hendrik,再次感谢您的回答!然后我将针对文档提交一个错误请求,因为它并没有明确说明第二段仅适用于 iOS4。 PS:您可能想在帖子中省略签名,请参阅this meta thread
    • rdar://11604456 提交,并在一个月后的即将发布的 iOS 6 版本中通过省略第二个稍微令人困惑的段落解决。
    • 谢谢!我在这个(以及其他相关问题)上苦苦挣扎了将近一个星期。这是第一个真正有意义且适用于 iOS5/iOS6 的答案。你是我的英雄;)
    • 非常感谢。此方案同样适用于iOS7.0.6。
    • 你摇滚!我使用的是[UIImage imageWithCGImage:[rep fullResolutionImage]],您的解决方案解决了旋转问题,谢谢! (适用于 iOS 8.1)
    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2017-08-19
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多