shelll
shelll

Reputation: 3373

Draw UIView with transform into UIImage

It is easy to draw a UIView into a UIImage with drawHierarchy(in: afterScreenUpdates:) and CALayer.render methods e.g.:

let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { rendererContext in
    view.drawHierarchy(in: view.frame, afterScreenUpdates: true)
}

But I want to render the UIView including its transform and/or its layer's transform. How can I achieve this? I need to render a UIView which is scaled and rotated.

One fallback solution is to render its superview into UIImage but for that I will have to change the UI hierarchy...

Upvotes: 0

Views: 756

Answers (1)

shelll
shelll

Reputation: 3373

There is a workaround for this issue. Wrap the the UIView with transform into another view (without transform) with frame of the "transformed view". Place the transformed view at .zero position in the wrapper view and call drawHierarchy(in:) (on the wrapper view).

Or in my specific case I needed to draw text views and I ended-up drawing the texts with Core Graphics directly.

Upvotes: 2

Related Questions