使用ALAssetOrientation和UIImageOrientation确定正确方向的问题

时间:2012-03-22 21:19:49

标签: objective-c uiimage orientation alasset uiimageorientation

我在iPhone上正确定位图像时遇到了问题。我正在使用AssetLibrary类访问图像。

所有,我想做的是正确定位图像以进行显示,遗憾的是,对于以纵向模式拍摄的一些图像,它们处于无意义的方向。

在框架调用的块中,我正在进行以下调用以获取iPhone摄像头拍摄的图片,同时它面向“向上”:

        ALAssetRepresentation   *representation = [myasset defaultRepresentation];
        ALAssetOrientation      orient          = [representation orientation];
        ALAssetOrientation      alorient = [[myasset valueForProperty:@"ALAssetOrientation"] intValue];
        UIImage                 *timg           = [[UIImage alloc] initWithCGImage:[representation fullScreenImage]];
        UIImageOrientation      imgorient       = [timg imageOrientation];

THe variable orient = ALAssetOrientationRight.
The variable alorient = ALAssetOrientationUp.
The variable imgorient = UIImageOrientationUp

我正在使用orient值来旋转图像,使其显示为“up”。不幸的是,如果我有一个以横向模式拍摄的图像,则相同的代码不起作用,因为方向不正确。

我尝试使用scale来实例化timg:orientation:使用orient作为orientation参数的重载,它只是初始化图像设置方向但错误地旋转图像。

这似乎是不确定的,为什么这些调用会返回不同的结果:

ALAssetOrientation      orient   = [representation orientation];
ALAssetOrientation      alorient = [[myasset valueForProperty:@"ALAssetOrientation"] intValue];

非常感谢您的帮助。

谢谢, 将

1 个答案:

答案 0 :(得分:0)

问题是您使用了错误的属性名称。

在您的代码中:

[[myasset valueForProperty:@"ALAssetOrientation"] intValue];

应该是:

[[myasset valueForProperty:@"ALAssetPropertyOrientation"] intValue];

根据-[ALAsset valueForProperty:]

documentation
If property is not a valid key, returns ALErrorInvalidProperty.

您的代码尝试将ALErrorInvalidProperty强制转换为int,结果为0。巧合ALAssetOrientationUp具有0的值,这就是为什么您的alorient变量将始终评估为ALAssetOrientationUp

相关问题