如何在CGImageMetadataRef上设置kCGImagePropertyExifUserComment?

时间:2017-06-13 18:56:33

标签: ios cgimageref

如何在iOS中的CGImageMetadataRef上设置kCGImagePropertyExifUserComment?这是我找到的唯一功能,它们仅适用于MacOS

https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.9.sdk/System/Library/Frameworks/ImageIO.framework/Versions/A/Headers/CGImageMetadata.h

2 个答案:

答案 0 :(得分:2)

根据图像数据,您可以使用以下

获取其属性
func getImageDataProperties(_ data: Data) -> NSDictionary? {
    if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil) 
    {
        if let dictionary = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) {
            return dictionary
        }
    }
    return nil
}

可以使用属性

按如下方式访问exif注释
if let properties = getImageDataProperties(data) {
    if let exif = properties[kCGImagePropertyExifDictionary] as? NSDictionary {
        if let comment = exif[kCGImagePropertyExifUserComment] as? String {
            ...
        }
    }
}

编辑完评论后,您可以使用以下内容保存图像,并提供原始图像数据和更改的属性字典

// add image properties (exif, gps etc) to image
func addImageProperties(imageData: Data, properties: NSMutableDictionary) -> Data? {

    // create an imagesourceref
    if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
        // this is of type image
        if let uti = CGImageSourceGetType(source) {

            // create a new data object and write the new image into it
            let destinationData = NSMutableData()
            if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {

                // add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
                CGImageDestinationAddImageFromSource(destination, source, 0, properties)
                if CGImageDestinationFinalize(destination) == false {
                    return nil
                }
                return destinationData as Data
            }
        }
    }
    return nil
}

答案 1 :(得分:0)

使用CGImageMetadataRef创建CGImageMetadataCreateMutableCopy的可变副本。然后使用

设置属性
CGImageMetadataSetValueMatchingImageProperty(
    mutableCopy, 
    kCGImagePropertyExifDictionary, 
    kCGImagePropertyExifUserComment, 
    value)

其中值是您作为CFTypeRef的注释。