更新UIImage方向元数据? [英] Updating UIImage orientation metaData?

查看:112
本文介绍了更新UIImage方向元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本任务:更新与UIImage关联的元数据的EXIF方向属性。我的问题是,我不知道所有EXIF信息中的方向属性。

Basic Task: update the EXIF orientation property for the metaData asociated with a UIImage. My problem is that I don't know where the orientation property is in all the EXIF info.

Convoluted背景:我正在改变 imagePickerController返回的图像的方向:didFinishPickingMediaWithInfo:所以我想我还需要在保存图像之前更新metaData writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata

Convoluted Background: I am changing the orientation of the image returned by imagePickerController:didFinishPickingMediaWithInfo: so I am thinking that I also need to update the metaData before saving the image with writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata.

换句话说,除非我改变它,metaData将包含旧/初始方向,因此是错误的。我改变方向的原因是因为当我运行核心图像面部检测程序时,它一直让我沮丧。使用前置摄像头在纵向模式下使用iPhone(设备)拍照时,方向为 UIImageOrientationRight (3)。如果我重写图像使方向为 UIImageOrientationUp (0),我会得到良好的人脸检测结果。作为参考,重写图像的例程如下。

In other words, unless I change it, the metaData will contain the old/initial orientation and thus be wrong. The reason I am changing the orientation is because it keeps tripping me up when I run the Core Image face detection routine. Taking a photo with the iPhone (device) in Portrait mode using the front camera, the orientation is UIImageOrientationRight (3). If I rewrite the image so the orientation is UIImageOrientationUp(0), I get good face detection results. For reference, the routine to rewrite the image is below.

整个相机定位的事情我觉得很混乱,我似乎在深入挖掘所有代码洞这个的。我看了帖子(这里此处这里。并根据这篇文章( https://stackoverflow.com/a/3781192/840992 ):

The whole camera orientation thing I find very confusing and I seem to be digging myself deeper into a code hole with all of this. I have looked at the posts (here, here and here. And according to this post (https://stackoverflow.com/a/3781192/840992):


相机实际上是风景原生的,所以当你以b
b在横向拍摄照片时,你可以向上或向下拍摄,当你拍摄一张价值b $ b的照片(取决于你持有设备)。

"The camera is actually landscape native, so you get up or down when you take a picture in landscape and left or right when you take a picture in portrait (depending on how you hold the device)."

......这完全令人困惑。如果上述情况属实,我认为我应该得到方向 UIImageOrientationLeftMirrored UIImageOrientationRightMirrored with t他前置摄像头。这些都不能解释为什么 CIDetector 未能通过选择器返回的原始图像。

...which is totally confusing. If the above is true, I would think I should be getting an orientation of UIImageOrientationLeftMirrored or UIImageOrientationRightMirrored with the front camera. And none of this would explain why the CIDetector fails the virgin image returned by the picker.

我正在接近这个屁股倒退,但我似乎无法定位......

I am approaching this ass-backwards but I can't seem to get oriented...

-(UIImage *)normalizedImage:(UIImage *) thisImage
{
    if (thisImage.imageOrientation == UIImageOrientationUp) return thisImage;

    UIGraphicsBeginImageContextWithOptions(thisImage.size, NO, thisImage.scale);
    [thisImage drawInRect:(CGRect){0, 0, thisImage.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}


推荐答案

看看我的回答这里:

强制UIImagePickerController以纵向/尺寸iOS拍照

github上的相关项目(你不需要运行项目,只需查看自述文件)。

and associated project on github (you won't need to run the project, just look at the readme).

它更关注阅读而不是编写元数据 - 但它包含一些关于Apple的imageOrientation和相应的方向'Exif'元数据的注释。

It's more concerned with reading rather than writing metadata - but it includes a few notes on Apple's imageOrientation and the corresponding orientation 'Exif' metadata.

这可能值得一读也

拍摄的照片会自动旋转在IOS 6.0或iPhone上传

有两种不同的常数编号约定来表示图像方向。

There are two different constant numbering conventions in play to indicate image orientation.


  1. 在TIFF / IPTC图像元数据标签中使用的kCGImagePropertyOrientation常量

  2. UIImage imageOrientation属性使用的UIImageOrientation常量。

iPhone原生相机方向是横向左侧(主页按钮位于右侧)。原始像素尺寸始终反映这一点,旋转标记用于正确定位图像的显示方向。

iPhones native camera orientation is landscape left (with home button to the right). Native pixel dimensions always reflect this, rotation flags are used to orient the image correctly with the display orientation.

Apple            UIImage.imageOrientation     TIFF/IPTC kCGImagePropertyOrientation

iPhone native     UIImageOrientationUp    = 0  =  Landscape left  = 1  
rotate 180deg     UIImageOrientationDown  = 1  =  Landscape right = 3  
rotate 90CCW      UIImageOrientationLeft  = 2  =  Portrait  down  = 8  
rotate 90CW       UIImageOrientationRight = 3  =  Portrait  up    = 6  

UIImageOrientation 4-7映射到 kCGImagePropertyOrientation 2,4,5,7 - 这些是镜像对应物。

UIImageOrientation 4-7 map to kCGImagePropertyOrientation 2,4,5,7 - these are the mirrored counterparts.

UIImage从底层的kCGImagePropertyOrientation标志派生它的imagerOrientation属性 - 这就是它是只读属性的原因。这意味着只要您正确获取元数据标记,imageOrientation就会正确显示。但是如果你正在阅读数字以便应用变换,你需要知道你正在看哪些数字。

UIImage derives it's imagerOrientation property from the underlying kCGImagePropertyOrientation flags - that's why it is a read-only property. This means that as long as you get the metadata flags right, the imageOrientation will follow correctly. But if you are reading the numbers in order to apply a transform, you need to be aware which numbers you are looking at.

这篇关于更新UIImage方向元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆