美文网首页
图片添加附加信息

图片添加附加信息

作者: Sweet丶 | 来源:发表于2021-08-17 10:34 被阅读0次

对于拍摄的图片,我们可以获取到图片的一些信息,比如下面方法可以获取:

    static func getImageExif(fromImageData imageData: Data) -> NSMutableDictionary? {
        guard imageData.count > 0 else {
            return nil
        }
        let options = [kCGImageSourceShouldCache : kCFBooleanFalse]
        guard let imgSrc = CGImageSourceCreateWithData(imageData as CFData, options as CFDictionary) else {
            return nil
        }
        let metadatacc = CFDictionaryCreateMutableCopy(nil, 0, CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options as CFDictionary)) as NSMutableDictionary
//        debugPrint("图片所有的exif的key:", metadatacc.allKeys)
        let exifdata = metadatacc//.value(forKey: kCGImagePropertyExifDictionary as String) as! NSMutableDictionary
        return exifdata
    }

我们可以对图片的附加信息进行修改以使得上传的图片自带自定义信息📌(只能使用系统提供的key去修改,不能新增自定义),关于更多可以通过kCGImagePropertyGPSDictionarykCGImagePropertyExifDictionary追踪到系统库中查找。

static func appendExifInfo(forImageData imageData: Data, exifInfo: [String : Any]) -> Data?{
        // 在图片中拿不到imageExif则说明图片有问题,不需要再添加
        guard let imageExif = self.getImageExif(fromImageData: imageData)  else {
            return imageData
        }
        debugPrint("图片初始的附加信息:", imageExif)
        let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)!// 因为上面的操作所有这里必有值
        guard let UTI = CGImageSourceGetType(imageSource) else {
            return imageData
        }
        debugPrint(UTI)
        // 组装好newExif
        let newExif: NSMutableDictionary = imageExif
        for (key, value) in exifInfo {
            newExif[key] = value
        }
        // 其他自定义信息
        let jsonAppendExif = "自定义的附加信息"
        let exifDic = newExif[kCGImagePropertyExifDictionary] as! NSMutableDictionary
        exifDic[kCGImagePropertyExifUserComment] = jsonAppendExif;
        
        // 创建句柄
        let newImgData = NSMutableData()
        guard let destination = CGImageDestinationCreateWithData(newImgData, UTI, 1, nil) else {
            return imageData
        }
        
        // 写入句柄
        CGImageDestinationAddImageFromSource(destination, imageSource, 0, newExif as CFDictionary)
        if CGImageDestinationFinalize(destination) {
            debugPrint("图片exif信息添加成功:", newExif)
            return newImgData as Data
        }
        
        debugPrint("图片exif信息添加失败")
        return imageData
    }

打印结果:

"图片初始的附加信息:" {
    ColorModel = RGB;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 534;
    PixelWidth = 401;
    ProfileName = "sRGB IEC61966-2.1";
    "{Exif}" =     {
        ColorSpace = 1;
        PixelXDimension = 401;
        PixelYDimension = 534;
    };
    "{JFIF}" =     {
        DensityUnit = 0;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        Orientation = 1;
    };
}
添加后的信息(比如设置了kCGImagePropertyGPSDictionary、kCGImagePropertyExifDictionary里面字典的kCGImagePropertyExifUserComment)
{
    ColorModel = RGB;
    Depth = 8;
    Orientation = 1;
    PixelHeight = 534;
    PixelWidth = 401;
    ProfileName = "sRGB IEC61966-2.1";
    "{Exif}" =     {
        ColorSpace = 1;
        PixelXDimension = 401;
        PixelYDimension = 534;
        UserComment = "自定义的附加信息";
    };
    "{GPS}" =     {
        Latitude = "22.57566166666667";
        LatitudeRef = N;
        Longitude = "114.0579";
        LongitudeRef = E;
    };
    "{JFIF}" =     {
        DensityUnit = 0;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        Orientation = 1;
    };
}

相关文章

  • 图片添加附加信息

    对于拍摄的图片,我们可以获取到图片的一些信息,比如下面方法可以获取: 我们可以对图片的附加信息进行修改以使得上传的...

  • 【前端】HTML属性

    介绍 HTML元素上的属性,可以在元素中添加附加信息。 可以在元素中添加附加信息 一般描述于开始标签 总是以名称/...

  • HTML 属性

    属性是 HTML 元素提供的附加信息。 HTML 属性 HTML 元素可以设置属性 属性可以在元素中添加附加信息 ...

  • HTML 属性

    属性是 HTML 元素提供的附加信息。 HTML 属性 HTML 元素可以设置属性 属性可以在元素中添加附加信息 ...

  • Swift--- 标签中带圆角,背景颜色,文字,图标的Label

    需求如图: Label只能添加图片attachment的富文本,但是不能直接在图片attachment中附加文字,...

  • Flask 返回图片及其图片的附加信息

    关于响应(Response) 视图函数的返回值会被自动转换为一个响应对象,Flask的转换逻辑如下: 1、如果返回...

  • swift3.0-ui-1.2添加字体类型

    接 UI-1 那篇 NSAttributedString类可以对字符串附加格式信息添加字体类型 可以看到指定部分...

  • html复习笔记

    HTML属性 1、给整个页面添加背景色--------bgcolor 2、拥有关于对齐方式的附加信息: 居中排...

  • 调试OSG 2个大晚上的3个问题,真是丢人

    : error LNK2019: 无法解析的外部符号 没什么,也许就是附加包含添加了,附加库添加了,但是对应头文件...

  • iOS 生成二维码图片

    一个特别简易的生成二维码的功能不需要添加任何库 附加小功能---判断图片中是否有二维码图片

网友评论

      本文标题:图片添加附加信息

      本文链接:https://www.haomeiwen.com/subject/aqtvbltx.html