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.





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()