Question

I've got a CAShapeLayer and was trying to draw it by passing it to CGContextDrawLayerAtPoint but get an error about passing a retainable parameter of CAShapeLayer *__strong to a function expecting a CGLayerRef.

I've done some browsing but can't figure out how to convert it/bridge the CAShapeLayer to the CGLayerRef.

If its not possible to convert, then can a CAShapeLayer be created using CGLayerCreateWithContext?

What I'm doing is creating a drawing with a UIBezierPath, then creating a CAShapeLayer and setting its path to the UIBezierPath then I want to display it. I could use addSublayer to display the shape layer however I'm going to display the same shape at multiple different points so rather than add multiple sublayers I was planning on using CGContextDrawLayerAtPoint to display it at different points.

Was it helpful?

Solution

Short answer: no.

Long answer: CAShapeLayer is a Core Animation layer (CALayer). You put it into your interface and it draws itself.

I know it's confusing, the way they use the word "layer", but CGContextDrawLayerAtPoint has nothing to do with that (it's using the word "layer" in a totally different way, referring to a CGLayer, which is completely different and is used utterly differently). You've gone down the wrong rabbit hole here.

I could use addSublayer: to display the shape layer

Not could. Must. That is what you do with a CAShapeLayer.

The alternative you are looking for would be to pull the CGPath out of the CAShapeLayer and assign that path and stroke it, multiple times in multiple places, in a CGContext. But there is really no need to do that. iOS drawing is all about layers; don't be afraid of having multiple sublayers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top