Lossy option has no effect when exporting PNG to HEIF

Under Sonoma 14.4 the compression option doesn't work with PNG images. It works for JPG/HEIF. Preview can export PNG file to HEIC with compression option. What am I missing? Previously this has worked. I am trying with 0.01 and 0.9 as compression quality and the file size is the same for PNG.

Is Preview using some trick to convert the image using ciContext.createCGImage?

PS: Compression option of 1.0 was broken under 14.4 RC and Preview created empty file.

func heifImageDataUsingDestination(at url: URL, compressionQuality : CGFloat) -> Data?  {
    
    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }
    guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil) else { return nil }
    var mutableData = NSMutableData()
    guard let imageDestination = CGImageDestinationCreateWithData(mutableData, "public.heic" as CFString, 1, nil) else { return nil }
    let options = [ kCGImageDestinationLossyCompressionQuality: compressionQuality ] as CFDictionary
    CGImageDestinationAddImage(imageDestination, cgImage, options)
    let success = CGImageDestinationFinalize(imageDestination)
    if success {
        return mutableData as Data
    }
    return nil
}
func heifImageDataUsingCIContext(at url: URL, compressionQuality : CGFloat) -> Data?  {
    
    guard let ciImage = CIImage(contentsOf: url) else { return nil }
    let context = CIContext()
    let colorspace = ciImage.colorSpace ?? CGColorSpaceCreateDeviceRGB()
    let options = [CIImageRepresentationOption(rawValue: kCGImageDestinationLossyCompressionQuality as String) : compressionQuality]
    return context.heifRepresentation(of: ciImage, format: .RGBA8, colorSpace: colorspace, options: options)
}

Replies

Same issue is observed with TIFF

I can confirm the issue (tested the Core Image API).

Interestingly, the heif10Representation(...) API still works as expected.

Same behaviour on 14.4.1. Not fixed.

  • Did you already file Feedback for this issue?

Add a Comment