UIGraphicsImageRenderer fails to render image from view

I have some uncontroversial code that used to work perfectly in iOS 12 (and I think 13, from memory):

Code Block
let cropRect = mapVC.view.frame.inset(by: mapVC.view.safeAreaInsets).inset(by: mapVC.mapEdgeInsets)
let mapRenderer = UIGraphicsImageRenderer(bounds: cropRect)
let img = renderer.image(actions:  { _ in
mapVC.view.drawHierarchy(in: mapVC.view.bounds, afterScreenUpdates: true)
})


Now, I'm getting a partially rendered image (only the top 8-10 px or so - the rest is white/blank), and this message in the debugger:

Code Block
[Unknown process name] vImageConvert_AnyToAny - failed width = 0 height = 1 dst component = 16bit float dstLayout = ByteOrder16Little dstBytesPerRow = 0 src component = 16bit integer srcLayout = ByteOrder16Little srcBytesPerRow = 0


There seems to be very little online about this error and I have no clue where to start with it.

The issue arises when calling .jpegData or .pngData methods on mapRenderer.

No other changes to the view hierarchy or the rendered view since this was working just fine.

Any suggestions?

Xcode 12.4, iOS 14.3 running on an iPhone XS Max.





  • On Sept. 11, 2022, I started getting the same kind of error messages.

    I had recently made changes on constraints for a PDFView, the source for my screen shots. In my case, I had these constraints in the ViewDidLoad. I had deleted the constraints for TOP and BOTTOM. When I added them back the errors went away, and I was able to get perfect screen shots and save them to the Photos app.

    I'm sure it was a "frame" issue created by bad or missing constraints.

    Let me know how it goes.

    --Jon

Add a Comment

Replies

Now, I'm getting a partially rendered image (only the top 8-10 px or so - the rest is white/blank), and this message in the debugger

-> same as you.

It seems that UIGraphicsImageRenderer causes this bug. And I solved it by old codes. How about this code for you?

let cropRect = mapVC.view.frame.inset(by: mapVC.view.safeAreaInsets).inset(by: mapVC.mapEdgeInsets)

UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 1.0)

mapVC.view.drawHierarchy(in: mapVC.view.bounds afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
Post not yet marked as solved Up vote reply of IeSo Down vote reply of IeSo