1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-11 05:05:23 +03:00

Fix #468: Convert node to an image without using a MacawView

This commit is contained in:
Yuri Strot 2018-11-01 19:04:14 +07:00
parent bbb9a60866
commit 20177d5ae4
2 changed files with 32 additions and 2 deletions

View File

@ -121,7 +121,27 @@ public extension Locus {
public extension CGAffineTransform {
public func toMacaw() -> Transform {
return Transform(m11: Double(a), m12: Double(b), m21: Double(c), m22: Double(d), dx: Double(tx), dy: Double(ty))
}
}
public extension Node {
public func toNativeImage(size: Size, layout: ContentLayout = .of()) -> UIImage {
let renderer = RenderUtils.createNodeRenderer(self, view: nil, animationCache: nil)
let rect = size.rect()
MGraphicsBeginImageContextWithOptions(size.toCG(), false, 1)
let ctx = MGraphicsGetCurrentContext()!
ctx.clear(rect.toCG())
let transform = LayoutHelper.calcTransform(self, layout, size)
ctx.concatenate(transform)
renderer.render(in: ctx, force: false, opacity: self.opacity)
let img = MGraphicsGetImageFromCurrentImageContext()
MGraphicsEndImageContext()
return img!
}
}

View File

@ -588,7 +588,7 @@ open class MacawView: MView, MGestureRecognizerDelegate {
}
}
private class LayoutHelper {
class LayoutHelper {
private var prevSize: Size?
private var prevRect: Rect?
@ -615,6 +615,16 @@ private class LayoutHelper {
return CGAffineTransform.identity
}
public class func calcTransform(_ node: Node, _ layout: ContentLayout, _ size: Size) -> CGAffineTransform {
if let canvas = node as? SVGCanvas {
return layout.layout(size: canvas.layout(size: size), into: size).toCG()
}
if let rect = node.bounds {
return layout.layout(rect: rect, into: size).toCG()
}
return CGAffineTransform.identity
}
public func nodeChanged() {
prevRect = nil
}