本文介绍了ALAsset中的Photo的方向行为不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个使用ALAsssetsLibrary来获取照片的应用。我已将照片放置在图像视图中,我可以上传到服务器。当我在拍摄一些照片后在真实设备上进行测试时,我发现应该在肖像中拍摄的照片变成风景。

I current have an app that uses ALAsssetsLibrary to fetch the photos. I have placed the photo to an image view and I am able to upload to the server. When I tested on the real device after taking some photos, I found out the photos that supposed to be taken in Portrait become a landscape.

因此,我调用了不同的功能得到这样的CGImage:

Therefore, I called different function to get the CGImage like this:

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

第一次试用,我用过这个:

The first tried out, I used this :

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

我认为那个有刻度和方向的那个可以给我正确的方向照片拍摄。但它没有给我正确的解决方案。

I thought the one with scale and orientation could give me the right orientation that the photo was taken. But it didn't give me the right solution.

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

Do I miss anything that is necessary to generate a correct orientation of photo?

推荐答案

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

The correct orientation handling depends on the iOS version you are using.On iOS4 and iOS 5 the thumbnail is already correctly rotated, so you can initialize your UIImage without specifying any rotation parameters.However for the fullScreenImage, the behavior is different for each iOS version. On iOS 5 the image is already rotated on iOS 4 not.

所以在iOS4上你应该使用:

So on iOS4 you should use:

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

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

On iOS5 the following code should work correctly:

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

干杯,

Hendrik

这篇关于ALAsset中的Photo的方向行为不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 08:39
查看更多