Showing posts with label context. Show all posts
Showing posts with label context. Show all posts

Sunday, 24 March 2013

Draw on iPhone/iPad screen using Bezier paths | Core Graphics - iOS App Development

UIBezierpath class can be used to draw line segments, rectangles, polygons, arcs, curves, oval shapes and a lot more using vector based method.

Note: We will be viewing all our drawings in an UIImageView object.

1) Let's create a rectangle by drawing line segments using Bezier paths.

Add/Modify highlighted part of code in your app.

ViewController.h

#import
@interface ViewController : UIViewController
{
UIImageView *drawpad;
}
@property (retain, nonatomic) IBOutlet UIImageView *drawpad;
@end


ViewController.m

@implementation ViewController
@synthesize drawpad;
- (void)viewDidLoad
{
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(50.0f, 50.0f)];
[path addLineToPoint:CGPointMake(270.0f, 50.0f)];
[path addLineToPoint:CGPointMake(270.0f, 500.0f)];
[path addLineToPoint:CGPointMake(50.0f, 500.0f)];
[path closePath];
UIGraphicsBeginImageContext(self.view.frame.size);
path.lineCapStyle = kCGLineCapRound;
path.lineWidth = 10.0f;
[[UIColor blackColor] setStroke];
[[UIColor redColor] setFill];
[path stroke];
[path fill];
self.drawpad.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}


Note: I have placed an UIImageView object in XIB and connected it to the drawpad outlet. The object's size is same as of view.

Run the app and you will see a rectangle drawn on your screen.

2) Let's see how to create an arc on screen

Modify the code in viewDidLoad function as

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
radius:30
startAngle:0
endAngle:(22.0f/7.0f)
clockwise:YES];
UIGraphicsBeginImageContext(self.view.frame.size);
path.lineCapStyle = kCGLineCapRound;
path.lineWidth = 10.0f;
[[UIColor blackColor] setStroke];
[path stroke];
self.drawpad.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


Run the app. An arc, part of circle with radius 30 and centre at (150,150), will be drawn with starting angle as 0 radian and ending angle as (22.0f/7.0f) radian.

For more info visit

Wednesday, 29 June 2011

How to take screenshot of the iPhone screen programmatically?

This post deals with taking screenshot of the iPhone screen with the help of coding.

Remember, when we say screen shot of the iPhone screen, we mean screenshot of any view which is visible on the iPhone screen. Also, a view is captured with all its subviews that are visible on screen at the time of capturing.

Add the following code at the place where you want to take screenshot of the view.

UIGraphicsBeginImageContext(view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

A Context, more accurately known as Graphics Context, is the drawing destination for your objects. A context can be a Bitmap image, a UIView layer, a PDF file or a Printer.

In the above code, view is the UIView object which has to be captured.

UIGraphicsBeginImageContext(view.frame.size) creates a bitmap image context and set it as the current context. It takes size as parameter. The image context made is of the same size as the size passed in as parameter to this function. Since, here we want the image context to be of same size as of view to be captured so we pass the size of view as parameter.

UIGraphicsGetCurrentContext() returns the current context for drawing. Since we already set the bitmap context as the current context so it will return the same bitmap context created above. Had the bitmap context not been set , all the drawings would have taken place on UIView layer context(which is the default current context).

[view.layer renderInContext:context] draws all the objects, on or above the view’s layer, to the context passed in as parameter.

UIGraphicsGetImageFromCurrentImageContext() returns an image based on the contents of the current bitmap-based graphics context.