'how to add AuiliaryData(kCGImageAuxiliaryDataTypeHDRGainMap) to a heic image?

I use the 'CGImageDestinationAddAuxiliaryDataInfo' try to add Auxiliary Data, this method can add a AVDepthData as AuxiliaryData to heic image. But, this method can't help me.

my code is:

func combo(jpgfile:String, hdrGainMap: String, to heicfile:String){
    let jpgImage = NSImage(contentsOfFile: jpgfile)
    let url = URL(fileURLWithPath: jpgfile)
    let cijpg = CIImage(contentsOf: url)
    let outputURL: URL? = URL(fileURLWithPath: heicfile)
    guard let cgImageDestination = CGImageDestinationCreateWithURL(outputURL! as CFURL, kUTTypeJPEG, 1, nil) else {
        return
    }

    let context = CIContext(options: nil)
    let dict: NSDictionary = [
       kCGImageDestinationLossyCompressionQuality: 1.0,
       kCGImagePropertyIsFloat: kCFBooleanTrue,
    ]
    if let cgImage: CGImage = context.createCGImage(cijpg!, from: cijpg!.extent) {
                CGImageDestinationAddImage(cgImageDestination, cgImage, nil)
    }
    var auxDataType: NSString?
    var auxDic = {kCGImageAuxiliaryDataInfoData}
    let gainurl = URL(fileURLWithPath: hdrGainMap)
    let gainImage = CIImage(contentsOf: gainurl)
    let cgGainImage = context.createCGImage(gainImage!, from: gainImage!.extent)
    let bytesPerRow = cgGainImage!.bytesPerRow
    let width = cgGainImage!.width
    let height = cgGainImage!.height
    let pixelFormat = cgGainImage?.pixelFormatInfo.rawValue
    let gainData = cgGainImage?.dataProvider?.data
    
    let auxDataDic = [kCGImageAuxiliaryDataInfoData:gainData,
           kCGImageAuxiliaryDataInfoDataDescription:["BytesPerRow":bytesPerRow, "width":width, "height":height,"Orientation":1,"pixelFormat":pixelFormat]
    ] as [String : Any]
    CGImageDestinationAddAuxiliaryDataInfo(cgImageDestination, kCGImageAuxiliaryDataTypeHDRGainMap, auxDataDic as CFDictionary)
    CGImageDestinationFinalize(cgImageDestination)
}

But,the code above will cause a runtime error:

(Fig) signalled err=-17102



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source