UIView should provide an API to retrieve its current snapshot as a UIImage

Originator:argentumko
Number:rdar://45031961 Date Originated:October 5, 2018
Status:Closed (Behaves correctly) Resolved:
Product:UIKit Product Version:
Classification:Suggestion Reproducible:
 
At the moment, the most efficient way to retrieve a UIView snapshot as an image via public API is to set up a Core Graphics bitmap context (directly or via UIGraphicsImageRenderer) and call drawViewHierarchyInRect:afterScreenUpdates: method. However, it looks like the internal implementation detail of this method actually retrieves such snapshot image and draws it to the current context. This may be inefficient in certain use cases: it requires additional memory allocation, rendering and potentially color conversion, which may not be necessary at all if the intended usage of the image is just saving it to a file on disk, or retaining it and rendering later. So it would be great if there was a public API method that skips the redrawing part, and just returns a UIImage.
Of course, the image returned by the similar -[UIView _imageFromRect:] SPI can be CGImage-based (on Simulator) or IOSurface-based (on device), but _drawViewHierarchyInRect: (which uses it) seems to handle this conversion, so it's reasonable to expect the proposed public method to do the same.
drawViewHierarchyInRect:afterScreenUpdates: deals with screen updates on demand, but even if the proposed method doesn't support screen updates (and returns nil if the view is not rendered yet), that seems like an acceptable compromise.

Comments

Apple Developer Relations

This issue behaves as intended based on the following: The fact that -drawViewHierarchyInRect: draws an image into the current context is an implementation detail. The image it draws comes from Core Animation and is not meant to be exposed to the app.

By argentumko at Oct. 10, 2018, 7:28 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!