Create CGImage from HEIF file

103 Views Asked by At

This is how I load a JPEG or PNG file into a CGImage:

imgDataProvider = CGDataProviderCreateWithFilename( [imgFilepath UTF8String] );

if( [[imgFilepath pathExtension] isEqualToString: @"png"] )
{
    image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
}
else if( [[imgFilepath pathExtension] isEqualToString: @"jpg"] || [[imgFilepath pathExtension] isEqualToString: @"jpeg"] )
{
    image = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
}

Unfortunaltey the is not a CGImageCreateWith... variant for HEIF

How do I create a CGImage from a HEIF encoded image file?

1

There are 1 best solutions below

0
On

It was easier than I thought

imageSrc = CGImageSourceCreateWithURL( (CFURLRef)[NSURL fileURLWithPath:imgFilepath], nil );
image = CGImageSourceCreateImageAtIndex( imageSrc, 0, nil );