gpt4 book ai didi

ios - 如何在 CGImageMetadataRef 上设置 kCGImagePropertyExifUserComment?

转载 作者:行者123 更新时间:2023-11-28 21:08:38 31 4
gpt4 key购买 nike

如何在 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

最佳答案

给定图像数据,您可以通过以下方式获得它的属性

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
}

关于ios - 如何在 CGImageMetadataRef 上设置 kCGImagePropertyExifUserComment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44529463/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com