CGImageMetadataTagCreate error when create gain map tag

I extracted the gain map info from an image using

let url = Bundle.main.url(forResource: "IMG_1181", withExtension: "HEIC")
let source = CGImageSourceCreateWithURL(url! as CFURL, nil)
let portraitData = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source!, 0, kCGImageAuxiliaryDataTypeHDRGainMap) as! [AnyHashable : Any]
let metaData = portraitData[kCGImageAuxiliaryDataInfoMetadata] as! CGImageMetadata

Then I printed all the metadata tags

func printMetadataProperties(from metadata: CGImageMetadata) {
    guard let tags = CGImageMetadataCopyTags(metadata) as? [CGImageMetadataTag] else {

        return
    }

    for tag in tags {
        if let prefix = CGImageMetadataTagCopyPrefix(tag) as String?,
            let namespace = CGImageMetadataTagCopyNamespace(tag) as String?,
           let key = CGImageMetadataTagCopyName(tag) as String?,
           let value = CGImageMetadataTagCopyValue(tag){
            print("Namespace: \(namespace), Key: \(key), Prefix: \(prefix), value: \(value)")
        } else {
            
        }
    }
}
//Namespace: http://ns.apple.com/ImageIO/1.0/, Key: hasXMP, Prefix: iio, value: True
//Namespace: http://ns.apple.com/HDRGainMap/1.0/, Key: HDRGainMapVersion, Prefix: HDRGainMap, value: 131072
//Namespace: http://ns.apple.com/HDRGainMap/1.0/, Key: HDRGainMapHeadroom, Prefix: HDRGainMap, value: 3.586325

I want to create a new CGImageMetadata and tags. But when it comes to the HDR tags. It always fails to add to metadata.

let tag = CGImageMetadataTagCreate(
        "http://ns.apple.com/HDRGainMap/1.0/" as CFString,  
        "HDRGainMap" as CFString,    
        "HDRGainMapHeadroom" as CFString,        
        .default,               
        3.56 as CFNumber                   
    ) 
let path = "\(HDRGainMap):\(HDRGainMapHeadroom)" as CFString
let success = CGImageMetadataSetTagWithPath(metadata, nil, path, tag)// always false

The hasXMP works fine. Is HDR a private dict for Apple?

CGImageMetadataTagCreate error when create gain map tag
 
 
Q