MacMini MacMini - 1 year ago 54
iOS Question

How to fix invalid context 0x0 with UIBezierPath?

I am trying to find the best way to implement an rounded rectangle (e.g. looling like the iphone icons). My search suggested using UIBezierPath.

In order to test that class I made a new xcode template (single view application) and basically just added the following lines in ViewController's viewDidLoad:

UIBezierPath* path = [UIBezierPath
bezierPathWithRoundedRect: CGRectMake(10, 10, 120, 120)
cornerRadius: 5];
[[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0] setFill];
[path stroke];
[path fill];

Now I get several "...invalid context 0x0 error...". I assume that I have to set a context first?! But how do I do this or if not fix those errors otherwise?

My search on that error came up with a few posts. Unfortunaltely all of them seemed to have rather complex coding associated. I'm pretty sure however that I have just a very basic misunderstanding here.

Thank you!

Answer Source

you can use this and assign that in layer of your view

UIBezierPath *maskpath=[UIBezierPath bezierPathWithRoundedRect:view1.bounds

   CAShapeLayer *maskLayer=[CAShapeLayer layer];



   [view1.layer addSublayer:maskLayer];