[go: up one dir, main page]

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.

kCMPhotoError_InvalidData

Well, there’s good news and bad news on that front. The good news is that you’ve found the underlying error. Based on my reading of the code I figured it’d be there.

The bad news is that that specific error, kCMPhotoError_InvalidData, has a wider range of origins, so it doesn’t lead to an obvious cause.

Graphics technologies aren’t really my métier, so I’m gonna ask some colleagues for input on this.

Share and Enjoy

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

Hey @jm_tressler,

Quinn pulled me in to take a look here :)

I have a theory about what the issue may be, but I'd like to take a look at a focused sample project that reproduces before I share it, would you be able to provide one?

--Greg

Hi Greg,

Yes, I'll work on creating an example sample project. Basically, I'm extracting an image from an ARSession, using the pixel buffer from an ARFrame.

I'm then converting the pixel buffer to a CIImage and then exporting it to an HEIC.

I should have something to share later today or early tomorrow.

Do I post it here?

Thanks

@jtressle,

Ok great! If you can post it somewhere and provide a link, that works. Otherwise, you can open a code-level support request: https://developer.apple.com/support/technical/

--Greg

Hi,

I'm having this same issue when trying to save HEIF using CIContext.

Have you been able to find a resolution to this issue?

Thanks,

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