How to render any UIView into a UIImage with Swift in iOS
Asked
Active
Viewed 285 times
-1
-
1Welcome to SO, we're here to _help_ not as a code writing service. What have you tried already? What issues are you facing with your current code? – Scriptable Dec 22 '17 at 08:44
-
See https://www.hackingwithswift.com/example-code/media/how-to-render-a-uiview-to-a-uiimage to learn how to capture the screen or part of it – Scriptable Dec 22 '17 at 08:45
-
Thanks for your link .It's right . – Sun Dec 22 '17 at 12:01
1 Answers
1
The code presented below can render any UIView
into a UIImage
:
let view = UIView()
let imageRenderer = UIGraphicsImageRenderer(size: view.bounds.size)
let renderedImage = imageRenderer.image {ctx in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
The second part of the task, sharing an image to a social platform, should be posted as a separate question.

Tzar
- 5,132
- 4
- 23
- 57
-
It's right .Thanks for give directions ."UIGraphicsImageRenderer' is only available on iOS 10.0 or newer" But how can user it on iOS 9. – Sun Dec 22 '17 at 12:25
-
1@Sun https://stackoverflow.com/questions/30696307/how-to-convert-a-uiview-to-an-image iOS9 solution – Scriptable Dec 22 '17 at 12:39