使用AV Foundation时,捕获图像的方向错误

时间:2014-02-20 03:10:26

标签: ios objective-c uiimage avfoundation calayer

我正在使用AV Foundation拍摄照片。用户拍照后,我将主图层的子图层内容属性设置为等于捕获的照片,即imageData:

subLayer.contents = (id)[UIImage imageWithData:imageData].CGImage;

这一切都很完美,照片会在屏幕上显示。

唯一的问题是照片显示器旋转了90度。

我知道如何才能正确显示照片而不是旋转照片吗?

以下是用户捕捉照片时照片的外观:

enter image description here

但接下来是我将它显示为子图层的样子:

enter image description here

3 个答案:

答案 0 :(得分:2)

UIImage *img = [UIImage imageWithData:imageData];
    UIImage *image_Corrected;
    if (img.imageOrientation !=UIImageOrientationUp)
    {

        image_Corrected=[UIImage imageWithCGImage:img.CGImage scale:0.0 orientation:UIImageOrientationUp];
    }
    else
    {
        image_Corrected=img;

    }

答案 1 :(得分:1)

如果设置的图像在设置时显示为旋转:

subLayer.contents = (id)[UIImage imageWithData:imageData].CGImage;

...你会发现这个没问题:

UIImageView *view = [UIImageView initWithImage:[UIImage imageWithData:imageData];
// then add view and display

UIImage或使用UIImage的类可以理解可能影响图像显示的元数据,如旋转。

因为您正在直接访问基础Quartz图像数据,所以您正在跳过UIImage显示旋转数据的部分。因此,要么尝试以另一种方式执行此操作(例如,使用UIImage设置背景图像),要么自己旋转CGImage(使用各种CG函数)。

答案 2 :(得分:1)

相机始终以相同方向拍摄照片,根据当前方向旋转或更改EXIF元数据字典是一项软件任务。
如果使用静态图像捕获或输出数据缓冲区,如何处理它们之间存在一些细微差别 我在博客上写了一篇关于它的文章:EXIF pain
我建议你也检查that sample code from Apple site,可能是AVCam中最完整的一个。