Unabe to use writeHEIFRepresentation - failed to add image to the PhotoCompressionSession.

I'm getting an error writing a ciImage as a heif image:

		// Create CIImage directly from pixel buffer
		let ciImage = CIImage(cvPixelBuffer: pixelBuffer, options: [CIImageOption.properties: combinedMetadata])
		
		// Write HEIC synchronously
		do {
			try ciContext.writeHEIFRepresentation(of: ciImage, to: url, format: .RGBA8, colorSpace: colorSpace)

The error I'm getting is:

Error Domain=CINonLocalizedDescriptionKey Code=3 "(null)" UserInfo={CINonLocalizedDescriptionKey=failed to write HEIC data to file., NSUnderlyingError=0x11b1a1ec0 {Error Domain=CINonLocalizedDescriptionKey Code=10 "(null)" UserInfo={CINonLocalizedDescriptionKey=failed to add image to the PhotoCompressionSession.}}}

Both

					try ciContext.writeJPEGRepresentation(of: copiedCIImage, to: url, colorSpace: colorSpace, options: options)

and

					try ciContext.writePNGRepresentation(of: copiedCIImage, to: url, format: .RGBA8, colorSpace: colorSpace)

work. I also verified that the code works with iOS 18.

Is there something new we need to do for Heif images?

Thanks in advance

When you get this error, do you see anything relevant in the system log? Specifically, look for log messages with a subsystem of com.apple.coreimage and a category of api.

See Your Friend the System Log for lots of hints and tips for wrangling the system log. In this case, however, the process is pretty simple:

  1. Launch Console.
  2. Make sure Action > Include {Info,Debug} Messages are checked.
  3. Select your iOS device on the left.
  4. Paste subsystem:com.apple.coreimage into the search box.
  5. And, after that, past category:api.
  6. Click “Start streaming”.
  7. And then reproduce the issue.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@DTS Engineer

Thanks for the info on the logging. Here is the error:

-[CIContext(ImageRepresentation) _CMPhotoRepresentationOfImage:depth:allowAlpha:containerFormat:colorSpace:options:error:] failed to add image to the PhotoCompressionSession. (kCMPhotoError_InvalidData)

I also caught the error and printed it out in Xcode:

Error Domain=CINonLocalizedDescriptionKey Code=3 "(null)" UserInfo={CINonLocalizedDescriptionKey=failed to write HEIC data to file., NSUnderlyingError=0x15b1295f0 {Error Domain=CINonLocalizedDescriptionKey Code=10 "(null)" UserInfo={CINonLocalizedDescriptionKey=failed to add image to the PhotoCompressionSession.}}}

This code works on iOS 18. Also, as stated above, I'm able to write the JPG and PNG versions of this image.

I was able to get a heif image created by using a CGImage and doing:

CGImageDestinationCreateWithURL CGImageDestinationAddImage CGImageDestinationFinalize

Hope this helps.

Unabe to use writeHEIFRepresentation - failed to add image to the PhotoCompressionSession.
 
 
Q