iOS中上传包含EXIF的图片和修改图片EXIF信息

很多小伙伴浏览图片网站的时候,会发现图片的一些拍摄参数,包括光圈、像素、镜头、相机等,这些信息就是EXIF信息。

什么是EXIF

EXIF(Exchangeable Image File)是“可交换图像文件”的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数、缩略图及其他属性信息。Exif标准最初由日本电子工业发展协会(JEIDA - Japan Electronic Industry Development Association)制订,目前的最新版本是发布于2010年04月的 Exif 2.3 版,该版本曾在2012年12月以及2013年5月有所修正,并已经应用到各个厂商的新影像设备中。
简单来说,Exif 信息就是由数码相机在拍摄过程中采集一系列的信息,然后把信息放置在我们熟知的 JPEG/TIFF 文件的头部,也就是说 Exif信息是镶嵌在 JPEG/TIFF 图像文件格式内的一组拍摄参数,它就好像是傻瓜相机的日期打印功能一样,只不过 Exif信息所记录的资讯更为详尽和完备。Exif 所记录的元数据信息非常丰富,主要包含了以下几类信息:

  • 拍摄日期
  • 拍摄器材(机身、镜头、闪光灯等)
  • 拍摄参数(快门速度、光圈F值、ISO速度、焦距、测光模式等)
  • 图像处理参数(锐化、对比度、饱和度、白平衡等)
  • 图像描述及版权信息
  • GPS定位数据
  • 缩略图

EXIF标签

iOS中上传包含EXIF的图片和修改图片EXIF信息_第1张图片

iOS中上传包含EXIF的图片和修改图片EXIF信息_第2张图片

iOS中如何取得图片的EXIF信息

要取得EXIF信息,首先要获取到图片的原始数据。iOS是通过UIImagePickerController这个类来获取图片的,下面我们就通过代码来说明。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    UIImage* tmpHeaderImg = [info objectForKey:UIImagePickerControllerEditedImage];
    NSURL *fileUrl = [info objectForKey:UIImagePickerControllerImageURL];
    //有的图片选择器返回的key是PHImageFileURLKey
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
    CFDictionaryRef imageInfo = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);
    //imageInfo 为图片全部EXIF信息 
    [self.image setImage:tmpHeaderImg];

}

代码写到这里用模拟器测试会发现,能获得图片的EXIF信息,但是一用真机运行就崩溃,这是为什么呢?
大家都知道,iOS上的APP只能取到沙盒里面的数据,而PHImageFileURLKey给出的地址是图片在手机里面的存储地址,APP是没有权限获取的。这时候我们就要用Asset来获取原始图片数据

[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
            
   CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
   NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
           
        }];

通过Asset获取图片适用于真机和模拟器,这样想服务器上传上述方法返回的imageData,就可以获得含有EXIF信息的图片了。如果不获取图片原始数据直接从图片选择器上传图片,苹果或基于安全和隐私方面的考虑,会隐藏大部分的EXIF信息。

iOS中上传包含EXIF的图片和修改图片EXIF信息_第3张图片
以下是上方图片的EXIF信息
Printing description of imageInfo:
{
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 3456;
    PixelWidth = 5184;
    ProfileName = "sRGB IEC61966-2.1";
    "{ExifAux}" =     {
        AFInfo =         (
            "0.4895833",
            "0.7025463",
            "0.02083333",
            "0.02488426",
            n,
            "0.3256173",
            "0.598235",
            "0.01697531",
            "0.03096065",
            n,
            "0.6574074",
            "0.598235",
            "0.01697531",
            "0.03096065",
            n,
            "0.2276234",
            "0.4845197",
            "0.01697531",
            "0.03096065",
            n,
            "0.4875579",
            "0.4810475",
            "0.02488426",
            "0.03790509",
            F,
            "0.7554013",
            "0.4845197",
            "0.01697531",
            "0.03096065",
            n,
            "0.3256173",
            "0.3708044",
            "0.01697531",
            "0.03096065",
            n,
            "0.6574074",
            "0.3708044",
            "0.01697531",
            "0.03096065",
            n,
            "0.4895833",
            "0.2725694",
            "0.02083333",
            "0.02488426",
            n
        );
        Firmware = "Firmware Version 1.1.1";
        FlashCompensation = 0;
        FocusMode = 1;
        ImageStabilization = 3;
        LensID = 51;
        LensInfo =         (
            18,
            135,
            0,
            0,
            18,
            135,
            0,
            0
        );
        LensModel = "Canon EF-S 18-135mm f/3.5-5.6 IS";
        SerialNumber = 331505805;
    };
    "{Exif}" =     {
        ApertureValue = 5;
        BodySerialNumber = 0331505805;
        ColorSpace = 1;
        ComponentsConfiguration =         (
            1,
            2,
            3,
            0
        );
        CustomRendered = 0;
        DateTimeDigitized = "2017:04:04 10:09:52";
        DateTimeOriginal = "2017:04:04 10:09:52";
        ExifVersion =         (
            2,
            3
        );
        ExposureBiasValue = 0;
        ExposureMode = 0;
        ExposureProgram = 3;
        ExposureTime = "0.01";
        FNumber = "5.6";
        Flash = 16;
        FlashPixVersion =         (
            1,
            0
        );
        FocalLength = 135;
        FocalPlaneResolutionUnit = 2;
        FocalPlaneXResolution = "5728.176470588235";
        FocalPlaneYResolution = "5808.403508771929";
        ISOSpeedRatings =         (
            100
        );
        LensModel = "EF-S18-135mm f/3.5-5.6 IS";
        LensSerialNumber = 000012e3b8;
        LensSpecification =         (
            18,
            135,
            0,
            0
        );
        MaxApertureValue = "5.656853725850966";
        MeteringMode = 5;
        PixelXDimension = 5184;
        PixelYDimension = 3456;
        RecommendedExposureIndex = 100;
        SceneCaptureType = 0;
        SensitivityType = 2;
        ShutterSpeedValue = "6.625";
        SubsecTime = 00;
        SubsecTimeDigitized = 00;
        SubsecTimeOriginal = 00;
        WhiteBalance = 0;
    };
    "{IPTC}" =     {
        Byline =         (
            ""
        );
        CopyrightNotice = "";
        StarRating = 0;
    };
    "{MakerCanon}" =     {
        CameraTemperatur = 15;
        ContinuousDrive = 0;
        FocusDistanceLower = "1.66";
        FocusDistanceUpper = "1.82";
        LensModel = "EF-S18-135mm f/3.5-5.6 IS";
        MaxAperture = "5.656854249492381";
        MinAperture = "35.91878554589994";
        UniqueModelID = 2147484295;
        WhiteBalanceIndex = 0;
    };
    "{PictureStyle}" =     {
        ColorTone =         (
            0,
            0,
            0
        );
        Monochrome =         (
            0,
            0,
            0
        );
        PictStyleColorSpace =         (
            sRGB,
            1,
            1
        );
        PictureStyle =         (
            4095,
            4095,
            4095
        );
        SharpnessFreq =         (
            0,
            0,
            0
        );
    };
    "{TIFF}" =     {
        Artist = "";
        Copyright = "";
        DateTime = "2017:04:04 10:09:52";
        Make = Canon;
        Model = "Canon EOS 60D";
        Orientation = 1;
        ResolutionUnit = 2;
        XResolution = 72;
        YResolution = 72;
    };
}

如何修改图片的EXIF

  1. 获取图片中的EXIF文件
NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];
NSMutableDictionary *exifDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
  1. 修改EXIF文件中的部分信息
[exifDic setObject:[NSNumber numberWithFloat:123123] forKey:(NSString *)kCGImagePropertyExifExposureTime];
[exifDic setObject:@"SenseTime" forKey:(NSString *)kCGImagePropertyExifLensModel];

[metaDataDic setObject:exifDic forKey:(NSString*)kCGImagePropertyExifDictionary];
  1. 将修改后的文件写入至图片中
CFStringRef UTI = CGImageSourceGetType(imageSource);
NSMutableData *newImageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newImageData, UTI, 1,NULL);

//add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic);
CGImageDestinationFinalize(destination);
  1. 保存图片
NSString *directoryDocuments =  NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];
  1. 查看修改后图片的Exif信息
CIImage *testImage = [CIImage imageWithData:newImageData];
NSDictionary *propDict = [testImage properties];
NSLog(@"Properties %@", propDict);

注意事项

关于无法修改Exif值的几点注意事项:

  1. 传入的数据格式与Exif规定的不符
    Exif的每条信息都有对应的数据类型,如:String Float... 如果数据类型传入错误将无法写入文件。
  2. 传入的字段超过规定字段长度
  3. 相互依赖的字段只添加了一个字段
    在GPS文件中经纬度的度数的字段与经纬度的方向的字段相互依赖,修改经/纬度数需要经/纬方向字段的存在,否则修改无效。

参考链接:https://www.jianshu.com/p/a542751d4ba3

你可能感兴趣的:(iOS中上传包含EXIF的图片和修改图片EXIF信息)