图片添加附加信息

2021-08-17  本文已影响0人  Sweet丶

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

    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;
    };
}
上一篇下一篇

猜你喜欢

热点阅读