CGImageSourceCreateThumbnailAtIndex returns image with black background instead of transparent

I have several CGImages with transparent background and save it to one HEIC image with Image I/O
Code Block swift
func heic(from images: [CGImage]) -> Data? {
guard
let mutableData = CFDataCreateMutable(kCFAllocatorDefault, 0),
let destination = CGImageDestinationCreateWithData(mutableData, "public.heic" as CFString, images.count, nil)
else { return nil }
for image in images {
CGImageDestinationAddImage(
destination,
image,
[
kCGImageDestinationEmbedThumbnail: true,
kCGImageDestinationLossyCompressionQuality: 1
] as CFDictionary
)
}
guard CGImageDestinationFinalize(destination) else { return nil }
return mutableData as Data
}

Then get thumbnail from HEIC data with CGImageSourceCreateThumbnailAtIndex
Code Block swift
func thumbnail(from data: Data, size: CGSize) -> CGImage? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else { return nil }
guard let context = CGContext(
data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue
) else { return nil }
for index in 0 ..< CGImageSourceGetCount(source) {
guard let image = CGImageSourceCreateThumbnailAtIndex(
source,
index,
[
kCGImageSourceCreateThumbnailFromImageIfAbsent: true,
kCGImageSourceThumbnailMaxPixelSize: Int(max(size.width, size.height))
] as CFDictionary
) else { continue }
context.draw(image, in: CGRect(origin: .zero, size: size))
}
return context.makeImage()
}

The problem is that resulting image has black background instead of transparent. I see this only on device (iPad Pro, iPadOS 14.2) not on simulator.

Also if I remove kCGImageDestinationEmbedThumbnail: true from destination properties, so that thumbnail not saved in Data, I receive correct thumbnail with transparent background from CGImageSourceCreateThumbnailAtIndex function.

What am I doing wrong? I want to have thumbnails in HEIC image and not create them in runtime on request.
CGImageSourceCreateThumbnailAtIndex returns image with black background instead of transparent
 
 
Q