Doug
Doug

Reputation: 869

iOS Newbie: Need help understanding Core Graphics Layers

A bit of a newbie question for iOS, just started learning about Core Graphics but I'm still not sure how it's integrated into the actual view

So I have a method that creates a CGLayer ref, draws some bezier curves on it, and then returns the CGLayer; here's a very condensed version of my procedure:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGLayerRef layer = CGLayerCreateWithContext(ctx, CGSizeMake(300.0,300.0), NULL);
CGContextRef layerCtx = CGLayerGetContext(layer);

Then I add some bezier fills that are dynamically generated by parsing an SVG file.

I've already verified that the SVG is being parsed properly, and I am using this code http://ponderwell.net/2011/05/converting-svg-paths-to-objective-c-paths/ to parse the path instructions (from the path tags' d parameters) into UIBezierPath objects

So I start by creating the fill

CGColorRef color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
CGContextSetFillColorWithColor(layerCtx, color);

Then adding the UIBezierPath

SvgToBezier *bezier = [[SvgToBezier alloc] initFromSVGPathNodeDAttr:pathAttr rect:CGRectMake(0, 0, 300, 300)];

UIBezierPath *bezierPath = bezier.bezier;

CGContextAddPath(layerCtx, bezierPath.CGPath);
CGContextDrawPath(layerCtx, kCGPathFill);

And finally I return the CGLayerRef like so

return layer;

My question: Now that I have this layer ref, I want to draw it to a UIView (or whatever type of object would be best to draw to)

The ultimate goal is to have the layer show up in a UITableViewCell.

Also, the method I've described above exists in a class I've created from scratch (which inherits from NSObject). There is separate method which uses this method to assign the layer ref to a property of the class instance.

I would be creating this instance from the view controller connected to my table view. Then hopefully adding the layer to a UITableView cell within the "cellForRowAtIndexPath" method.

I hope I included all of the relevant information and I greatly appreciate your help.

Cheers

Upvotes: 0

Views: 565

Answers (1)

diatrevolo
diatrevolo

Reputation: 2822

Short answer is the view you want to draw on's layer needs to be set to your newly created layer.

[myCurrentViewController.view setLayer:newLayer];

Long answer can be found in the excellent Quartz2D programming guide, found here.

Upvotes: 1

Related Questions