Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing UIBezierPath on view

In one of my methods i have this code:

-(void)myMethod {   
   UIBezierPath *circle = [UIBezierPath
                       bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)];  
}

How do i get it to show on the view?

I tried addSubview but it gave me an incompatible type error because its expecting a UIView.

I'm sure this must be simple.

Thanks

like image 946
joec Avatar asked Jul 28 '10 11:07

joec


People also ask

What is UIBezierPath?

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. After you have the object configured the way you want it, you can tell it to draw itself in the current context.

What is CAShapeLayer in Swift?

A layer that draws a cubic Bezier spline in its coordinate space.


1 Answers

Just thought I'd add that you don't have to necessarily draw this in a UIView's "drawRect:" method. You can draw it anywhere you'd like to provided you do it inside of a UIGraphics image context. I do this all of the time when I don't want to create a subclass of UIView. Here's a working example:

UIBezierPath *circle = [UIBezierPath
                        bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)];  

//you have to account for the x and y values of your UIBezierPath rect
//add the x to the width (75 + 200)
//add the y to the height (100 + 200)
UIGraphicsBeginImageContext(CGSizeMake(275, 300));

//this gets the graphic context
CGContextRef context = UIGraphicsGetCurrentContext();

//you can stroke and/or fill
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
[circle fill];
[circle stroke];

//now get the image from the context
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage];

Now just add the UIImageView as a subview.

Also, you can use this for other drawing too. Again, after a little bit of setup, it works just like the drawRect: method.

//this is an arbitrary size for example
CGSize aSize = CGSizeMake(50.f, 50.f);

//this can take any CGSize
//it works like the frame.size would in the drawRect: method
//in the way that it represents the context's size
UIGraphicsBeginImageContext(aSize);

//this gets the graphic context
CGContextRef context = UIGraphicsGetCurrentContext();

//you can do drawing just like you would in the drawRect: method
//I am drawing a square just for an example to show you that you can do any sort of drawing in here
CGContextMoveToPoint(context, 0.f, 0.f);
CGContextAddLineToPoint(context, aSize.width, 0.f);
CGContextAddLineToPoint(context, aSize.width, aSize.height);
CGContextAddLineToPoint(context, 0.f, aSize.height);
CGContextClosePath(context);

//you can stroke and/or fill
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextDrawPath(context, kCGPathFillStroke);

//now get the image from the context
UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage];

Edit: One thing I should add is that for any modern day drawing of this kind, you should be swapping out

UIGraphicsBeginImageContext(size);

for

UIGraphicsBeginImageContextWithOptions(size, opaque, scale);

This will draw your graphics correctly for retina and non retina displays.

FYI, UIGraphicsBeginImageContext(size) is equivalent to UIGraphicsBeginImageContextWithOptions(size, FALSE, 1.f) which is fine for none retina displays that may have some transparency.

However, if you don't need transparency, it is more optimized to pass in TRUE for the opaque argument.

The safest and recommended way of drawing is to pass in [[UIScreen mainScreen]scale] as the scale argument.

So for the example(s) above, you would use this instead:

UIGraphicsBeginImageContextWithOptions(aSize, FALSE, [[UIScreen mainScreen] scale]);

For more info, check out Apple's docs.

like image 51
daveMac Avatar answered Oct 23 '22 10:10

daveMac