HomeiOS Developmentios - Cannot get CALayer or UIView from SwiftSVG to render to...

ios – Cannot get CALayer or UIView from SwiftSVG to render to UIImage with UIGraphicsImageRenderer()


I am making an attempt to determine why I can not render a UIView or a CALayer as a UIImage, when the originating CALayer (which is legitimate!) is generated by the github-hosted SwiftSVG framework.

Sadly, SwiftSVG doesn’t present an choice for making a UIImage, … solely a CALayer or a UIView

And I can not seem to render that CALayer right into a UIImage.

My supply .svg file defines a viewBox 1169 x 1000.

I see the SVG graphic once I create CALayer with SwiftSVG from .svg file, use SwiftSVG’s resizeToFit() (which applies an affine scaling remodel on layer).

(after I add CALayer as sublayer of UIView, and UIView as subview of view controller’s view)

The layer is okay, and renders a gorgeous graphic:

    let viewBox : CGRect? = CGRectMake(0, 0, 116.9, 100)
    let filename =  (fieldKey + ".svg").lowercased()
    let svgURL = gbl_svgDataUrl.appendingPathComponent(filename)

    // vvv THIS WORKS: Retrieves legitimate CALayer with SVG graphic from SwiftSVG:

    let svgLayer = CALayer(SVGURL: svgURL) { (svgLayer) in
        svgLayer.fillColor = colour.cgColor
        if let viewBox {
            svgLayer.resizeToFit(viewBox)
        }
    }

Nevertheless, when I attempt to render the identical layer as a picture (and explicitly not including the layer on to the view hierarchy as earlier than), the SVG graphics do not render.

To make sure UIGraphicImageRenderer is working, I subsequently added some drawing instructions within the closure, to attract a top level view across the rendering space.

The drawing instructions works. The drawn define reveals up within the UIGraphicImageRenderer-produced picture. Nevertheless, the SVG-rendered graphics don’t.

svgLayer.render() doesn’t produce a graphic within the produced picture.

Nor does svgView.drawHierarchy() work if I add the CALayer as a sublayer of the view.

To rule out issues with SwiftSVG’s resizeToFit() affine remodel, I’ve tried rendering each with and with out resizing from inside SwiftSVG.

In conclusion: CALayer sublayer of a UIView seems on display high-quality.
UIImage generated from the identical CALayer doesn’t.

Any concepts what I ought to do or test or what is perhaps occurring, even in a normal sense?

    let format = UIGraphicsImageRendererFormat.most well-liked()
    format.opaque = false
    let picture = UIGraphicsImageRenderer(measurement: viewBox.measurement, format: format).picture { renderer in
        let ctx = renderer.cgContext
        UIColor.purple.setFill()
        UIColor.inexperienced.setStroke()
        ctx.setLineWidth(0.5)
        ctx.stroke(renderer.format.bounds)       // <-- works

        svgLayer.render(in: renderer.cgContext)  // <-- does not work

        //svgView.drawHierarchy(in: viewBox,     // <-- does not work
        //   afterScreenUpdates: false)           
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments