【问题标题】:Orientation does not behave correctly with Photo in ALAsset使用 ALAsset 中的照片时,方向的行为不正确
【发布时间】:2012-02-08 14:13:18
【问题描述】:

我目前有一个使用 ALAsssetsLibrary 来获取照片的应用。我已将照片放置到图像视图中,并且可以上传到服务器。拍了几张之后在真机上测试,发现本来应该拍人像的照片变成了风景。

因此,我调用了不同的函数来获取这样的 CGImage:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0 orientation:(UIImageOrientation)[representation orientation]];

第一次试用,我用这个:

UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]]

我认为带有比例和方向的那个可以为我提供正确的拍摄方向。但它没有给我正确的解决方案。

我是否遗漏了生成正确照片方向所需的任何内容?

【问题讨论】:

  • 从设备拍摄的照片会出现此问题

标签: iphone image orientation alassetslibrary


【解决方案1】:

试试这个代码:-

 UIImage* img = [UIImage imageWithCGImage:asset.thumbnail];
 img = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationUp];

这可能会对你有所帮助。

【讨论】:

  • 我现在无法尝试,因为我不拥有该设备。你介意给我解释一下这两行代码吗?第一行的 img 是资产的缩略图,而不是实际图像。
  • 我使用了图像的缩略图,并且我保持图像的方向向上,因此它不会被旋转它在我的情况下工作。
  • 嗯,我在使用 iPhone 的 UIImageOrientationUp 时遇到了问题。它适用于 iPad,但不适用于 iPhone。这是否意味着两个设备上的 UIImageOrientationUp 不同。
  • 它只是让你的图像向上定向。
【解决方案2】:

正确的方向处理取决于您使用的 iOS 版本。 在 iOS4 和 iOS 5 上,缩略图已经正确旋转,因此您可以在不指定任何旋转参数的情况下初始化 UIImage。 但是对于 fullScreenImage,每个 iOS 版本的行为都不同。在 iOS 5 上,图像已经在 iOS 4 上没有旋转。

所以在 iOS4 上你应该使用:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                                     scale:[defaultRep scale] orientation:(UIImageOrientation)[defaultRep orientation]];

在 iOS5 上,以下代码应该可以正常工作:

ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
UIImage *_image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

干杯,

亨德里克

【讨论】:

  • 在我的情况下,我需要 fullResolutionImage 并在 iOS 6 和 7 上进行了测试。图像不会自动旋转,但第一个示例代码可以解决问题!谢啦!附言如果将图像保存到带有元数据的文件,请小心。如果您已旋转图像,则必须将 EXIF 方向设置为 0 以避免奇怪的旋转 jpeg ;)
  • 我必须更正我的评论。第一个代码示例没有旋转我的 fullResolution 图像,我必须自己旋转它。但同意 fullScreenImage 在 iOS 6 和 7 上自动旋转。
  • 太棒了!它也适用于 iOS 7 !非常感谢!
【解决方案3】:

我的经验仅限于 IOS 5.x,但我可以告诉您缩略图和全屏图像的方向正确。垂直拍摄时,它是水平的全分辨率图像。我的解决方案是使用从这里获得的 uiimage 类别:

http://www.catamount.com/forums/viewtopic.php?f=21&t=967&start=0

它在 UIImage 上提供了一个很好的旋转方法,如下所示:

        UIImage *tmp = [UIImage imageWithCGImage:startingFullResolutionImage];
        startingFullResolutionImage = [[tmp imageRotatedByDegrees:-90.0f] CGImage];

【讨论】:

  • 我刚刚使用了 fullresolutionimage,它对我有用。谢谢。
【解决方案4】:

对于fullResolutionImage,我想提供如下解决方案,

ALAssetRepresentation *rep = [asset defaultRepresentation];

// First, write orientation to UIImage, i.e., EXIF message.
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage] scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
// Second, fix orientation, and drop out EXIF
if (image.imageOrientation != UIImageOrientationUp) {
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
   [image drawInRect:(CGRect){0, 0, image.size}];
   UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   image = normalizedImage;
}
// Third, compression
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

imageData 是您想要的,只需将其上传到您的照片服务器即可。

顺便说一句,如果你觉得 EXIF 有用,可以随意补充到normalizedImage

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多