Quartz 2D Programming Guide -Pattern

参考

[1]http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html

[2]raywenderlish Core Graphics 101: 模版


Pattern 可以翻译为模板,或者图章,类似与photoshop中图章,作为一个基本的绘制单元,可以在画布上不断使用的图章,绘制出相同内容。

Pattern根据在创建Pattern阶段是否设置相应颜色,可以分为Colored Patterns and Stencil (Uncolored) Patterns

Colored Patterns 中颜色是Pattern创建阶段设置,而不是在Pattern绘制阶段设置。

Stencil (Uncolored) Patterns 中颜色不在Pattern创建阶段设置,而是在Pattern绘制阶段设置。


根据Pattern绘制到context时的 效率和是否失真,绘制方式有如下三种

kCGPatternTilingNoDistortion
kCGPatternTilingConstantSpacingMinimalDistortion
kCGPatternTilingConstantSpacing


绘制Colored Patterns 例子如下

创建Pattern相关代码,注释部分是创建RGB的小色块,未注释部分是创建五角星,并填充红色。MyDrawColoredPattern作为创建CGPatternCreate是设定的回掉函数。

#define H_PATTERN_SIZE 16
#define V_PATTERN_SIZE 18

#define PSIZE 16    // size of the pattern cell


void MyDrawColoredPattern (void *info, CGContextRef myContext)
{
	/*
	CGRect* rc = (CGRect*)info;
	
    CGFloat subunit = 5; // the pattern cell itself is 16 by 18
	
    CGRect  myRect1 = {{0,0}, {subunit, subunit}},
	myRect2 = {{subunit, subunit}, {subunit, subunit}},
	myRect3 = {{0,subunit}, {subunit, subunit}},
	myRect4 = {{subunit,0}, {subunit, subunit}};

    CGContextSetRGBFillColor (myContext, 0, 0, 1, 0.5);
    CGContextFillRect (myContext, myRect1);
    CGContextSetRGBFillColor (myContext, 1, 0, 0, 0.5);
    CGContextFillRect (myContext, myRect2);
    CGContextSetRGBFillColor (myContext, 0, 1, 0, 0.5);
    CGContextFillRect (myContext, myRect3);
    CGContextSetRGBFillColor (myContext, .5, 0, .5, 0.5);
    CGContextFillRect (myContext, myRect4);

	CGContextSetRGBStrokeColor(myContext, 1.0, 1.0,1.0, 1.0);
	CGContextStrokeRect(myContext, *rc);
	*/
	int k;
    double r, theta;
	
    r = 0.8 * PSIZE / 2;
    theta = 2 * M_PI * (2.0 / 5.0); // 144 degrees
	
	CGContextSetRGBFillColor (myContext, 1, 0, 0, 1.0);

    CGContextTranslateCTM (myContext, PSIZE/2, PSIZE/2);
	
    CGContextMoveToPoint(myContext, 0, r);
    for (k = 1; k < 5; k++) {
        CGContextAddLineToPoint (myContext,
								 r * sin(k * theta),
								 r * cos(k * theta));
    }
    CGContextClosePath(myContext);
    CGContextFillPath(myContext);
}
MyColoredPatternPainting方法可以在UIView的DrawRect中调用,其中每行的含义解释如下

- (void) MyColoredPatternPainting:(CGContextRef) myContext rect:(CGRect) rect


{
    CGPatternRef    pattern;// 1
    CGColorSpaceRef patternSpace;// 2
    CGFloat         alpha = 1.0;// 3
	//width, height;// 4
    static const    CGPatternCallbacks callbacks = {0, // 5
		&MyDrawColoredPattern,
		NULL};
	
    CGContextSaveGState (myContext);
    patternSpace = CGColorSpaceCreatePattern (NULL);// 6
    CGContextSetFillColorSpace (myContext, patternSpace);// 7
	CGContextSetStrokeColorSpace(myContext, patternSpace);//
    CGColorSpaceRelease (patternSpace);// 8
	
	NSLog(@">>>%p ",&rect);
    pattern = CGPatternCreate (&rect, // 9
							   CGRectMake (0, 0, H_PATTERN_SIZE, V_PATTERN_SIZE),// 10
							   CGAffineTransformIdentity,//CGAffineTransformMake (1, 0, 0, 1, 0, 0),// 11
							   H_PATTERN_SIZE, // 12
							   V_PATTERN_SIZE, // 13
							   kCGPatternTilingConstantSpacing,// 14
							   true, // 15
							   &callbacks);// 16
	
    CGContextSetFillPattern (myContext, pattern, &alpha);// 17
	CGPatternRelease (pattern);// 18
    CGContextFillRect (myContext, rect);// 19
	CGContextRestoreGState (myContext);
	

}


1.Declares storage for a CGPattern object that is created later. 声明CGPattern 变量
2.Declares storage for a pattern color space that is created later. 声明PatternColor 变量,
3.Declares a variable for alpha and sets it to 1, which specifies the opacity of the pattern as completely opaque.
4.Declares variable to hold the height and width of the window. In this example, the pattern is painted over the area of a window. 
5.Declares and fills a callbacks structure, passing 0 as the version and a pointer to a drawing callback function. This example does not provide a release info callback, so that field is set toNULL.
6.Creates a pattern color space object, setting the pattern’s base color space toNULL. When you paint a colored pattern, the pattern supplies its own color in the drawing callback, which is why you set the color space toNULL.
7.Sets the fill color space to the pattern color space object you just created.
8.Releases the pattern color space object.
9.Passes NULL because the pattern does not need any additional information passed to the drawing callback.
10.Passes a CGRect object that specifies the bounds of the pattern cell.  模板的大小
11.Passes a CGAffineTransform matrix that specifies how to translate the pattern space to the default user space of the context in which the pattern is used. This example passes the identity matrix. 默认的 几何变换矩阵,不想设置变换,可以设置单位矩阵
12.Passes the horizontal pattern size as the horizontal displacement between the start of each cell. In this example, one cell is painted adjacent to the next. 每个模板直接的间距,如果设置的大小小于模板大小,那么模板会叠在一起。
13.Passes the vertical pattern size as the vertical displacement between start of each cell.
14.Passes the constant kCGPatternTilingConstantSpacing to specify how Quartz should render the pattern. For more information, see“Tiling.” 模板绘制到context上的方式,效率和是否失真考虑。
15.Passes true for the isColored parameter, to specify that the pattern is a colored pattern.
16Passes a pointer to the callbacks structure that contains version information, and a pointer to your drawing callback function. 传入 一个结构体,里面包含绘制pattern的回掉函数
17.Sets the fill pattern, passing the context, the CGPattern object you just created, and a pointer to the alpha value that specifies an opacity for Quartz to apply to the pattern.  可以设置应用pattern时的透明度
18.Releases the CGPattern object.
19.Fills a rectangle that is the size of the window passed to the MyColoredPatternPainting routine. Quartz fills the rectangle using the pattern you just set up.


CGPatternCreate 传入CGPatternCallbacks 的回掉函数结构如下,version表示版本,CGPatternDrawPatternCallback是绘制pattern的回掉函数,releaseInfo 是释放相应数据的回掉函数,特别是 传入

CGPatternCreate的第一个参数。


struct CGPatternCallbacks{    unsigned int version;    CGPatternDrawPatternCallback drawPattern;    CGPatternReleaseInfoCallback releaseInfo;};

Stencil (Uncolored) Patterns  的绘制类似于color Patterns 。

上述结果如下

Quartz 2D Programming Guide -Pattern_第1张图片


   

你可能感兴趣的:(Quartz 2D Programming Guide -Pattern)