gpt4 book ai didi

objective-c - 如何从 ALAssetRepresentation 获取正确旋转的 UIImage?

转载 作者:可可西里 更新时间:2023-11-01 05:04:49 24 4
gpt4 key购买 nike

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

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.

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.

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

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

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

最佳答案

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以获得更深入的解释。

关于objective-c - 如何从 ALAssetRepresentation 获取正确旋转的 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10879820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com