Rendering UIView into UIImage

Oct. 2, 2018, 11:47 p.m.

Hey guys!

If you are stuck with "render(in:)" function in Swift, this fix might help you.
Apple shows this example of rendering an image:

let renderer = UIGraphicsImageRenderer(size: rect.size)
let image = renderer.image {
context in
return shapeLayer.render(in: context.cgContext)
}

Unfortunately in my case, it was working incorrectly.
But this DID work:

// in UIView
let renderer = UIGraphicsImageRenderer(size: self.bounds.size)
let image = renderer.image {
context in
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
}

Image didn't have extra black rectangles anymore. And it rendered correctly.

Maybe it will help someone. ;)