The most frequently used examples in Quartz

Quartz Demo

Open context:

1
2
3
4
5
6
// Create context
UIGraphicsBeginImageContext(itemImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();

//Draw image in context
CGContextDrawImage(context, imageRect, [itemImage CGImage]);

Draw square in context:

1
2
3
4
5
CGContextSaveGState(context);
// Set color to context for draw
CGContextSetRGBFillColor(context, 0.4f, 0.4f, 0.4f, 0.4f);  // gray color with alpha 40 %
CGContextFillRect(context, rect);                               // draw in rect of square
CGContextRestoreGState(context);

Draw text in context:

1
2
3
4
5
6
7
8
9
10
NSString *text = @"Hello world!";
UIFont* font = [UIFont fontWithName:@"Helvetica" size:14];

// Open
UIGraphicsPushContext(context);
// Color for text
[[UIColor whiteColor] set]; // or CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
[text drawInRect: CGRectMake(2.0f, 0.0f, 30.0f, 30.0f) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
// Close
UIGraphicsPopContext();

Use CGContextSetShadow for draw shadow, example:

1
2
CGSize myShadowOffset = CGSizeMake (-15,  20);
CGContextSetShadow (context, myShadowOffset, 5);

Quartz 2D Programming Guide – pdf file from “iPhone Dev Center”