CoreGraphics画图iOS片断:
//线的接合
-(void)drawRooftopAtTopPointof:(CGPoint)paramTopPoint textToDisplay:(NSString *)paramText lineJoin:(CGLineJoin)paramLineJoin{
[[UIColorbrownColor]set];
CGContextRef currentContext =UIGraphicsGetCurrentContext();
CGContextSetLineJoin(currentContext,paramLineJoin);
CGContextSetLineWidth(currentContext, 20.0f);
CGContextMoveToPoint(currentContext, paramTopPoint.x-140, paramTopPoint.y+100);
CGContextAddLineToPoint(currentContext, paramTopPoint.x, paramTopPoint.y);
CGContextAddLineToPoint(currentContext, paramTopPoint.x+140, paramTopPoint.y+100);
CGContextStrokePath(currentContext);
[[UIColorbrownColor]set];
[paramText drawAtPoint:CGPointMake(paramTopPoint.x-40.0f, paramTopPoint.y+60.0f)withFont:[UIFontboldSystemFontOfSize:30.0f]];
}
- (void)drawRect:(CGRect)rect
{
//画字符串
UIColor *magentaColor =[UIColorcolorWithRed:0.5green:0.0f blue:0.5f alpha:1.0f];
[magentaColor set];
UIFont *helve=[UIFontfontWithName:@"HelveticaNeue-Bold"size:40.0f];
NSString *myString =@"Some String";
//[myString drawAtPoint:CGPointMake(40, 180) withFont:helve];
[myString drawInRect:CGRectMake(100,120, 150, 200) withFont:helve];
//color reference
CGColorRef colorRef =[magentaColor CGColor];//CGColorRef 的对象
const CGFloat *components =CGColorGetComponents (colorRef);//颜色对象的各个分量
NSUInteger componentsCount =CGColorGetNumberOfComponents(colorRef);//颜色对象的各个分量的数量
NSUInteger counter =0;
for (counter =0; counter<componentsCount; counter++) {
NSLog(@"compoent %lu =%0.02f",(unsignedlong )counter +1,components[counter]);
}
//png drawRect
UIImage *imageIcon= [UIImageimageNamed:@"xcode"];
if(imageIcon!=nil){
NSLog(@"Successfully loaded the imaged");
[imageIcon drawAtPoint:CGPointMake(0,20)];
[imageIcon drawInRect:CGRectMake(50.0f,10.0f, 40.0f,35.0f)];
}else {
NSLog(@"failed to load the image");
}
//drawLine
[[UIColorbrownColor]set];
CGContextRef currentContext =UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 5.0f);
CGContextMoveToPoint(currentContext, 20.0f, 20.0f);
CGContextAddLineToPoint(currentContext, 100, 100.0f);
CGContextAddLineToPoint(currentContext, 100.0f ,100.0f);
CGContextAddLineToPoint(currentContext, 300.0f, 100.0f );
CGContextStrokePath(currentContext);
//Linejoin 线的接合处
[selfdrawRooftopAtTopPointof:CGPointMake(160.0f,40.0f) textToDisplay:@"尖角连接"
lineJoin:kCGLineJoinMiter];//尖角连接
[selfdrawRooftopAtTopPointof:CGPointMake(160.0f,180.0f) textToDisplay:@"平角"
lineJoin:kCGLineJoinBevel];//平角
[selfdrawRooftopAtTopPointof:CGPointMake(160.0f,320.0f) textToDisplay:@"圆形"
lineJoin:kCGLineJoinRound]; // 圆形
//Draw Path
CGMutablePathRef path =CGPathCreateMutable();
CGRect screenBounds =[[UIScreenmainScreen]bounds];
CGPathMoveToPoint(path, NULL, screenBounds.origin.x, screenBounds.origin.y);
CGPathAddLineToPoint(path, NULL, screenBounds.size.width, screenBounds.size.height);
CGPathMoveToPoint(path, NULL, screenBounds.size.width, screenBounds.origin.y);
CGPathAddLineToPoint(path, NULL, screenBounds.origin.x, screenBounds.size.height);
currentContext =UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 4.0f);
CGContextAddPath(currentContext, path);
[[UIColorblueColor ]setStroke];
CGContextDrawPath(currentContext,kCGPathStroke);
CGPathRelease(path);
//Draw rectangle
CGMutablePathRef path1=CGPathCreateMutable();//Create the path first. Just the path handle
CGRect rectangle =CGRectMake(10,10, 20, 30);
CGPathAddRect(path1,NULL, rectangle);//Add the rectangle to the path1
CGContextRef currentContext1=UIGraphicsGetCurrentContext();/* Get the handle to the current context */
CGContextAddPath(currentContext1, path);// Add the path to the context
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];//Set the fill color to cornflower blue
[[UIColorbrownColor]setStroke];// Set the stroke color to brown
CGContextSetLineWidth(currentContext1, 5.0f);
CGContextDrawPath(currentContext1,kCGPathFillStroke);//Stroke and fill the path on the context
CGPathRelease(path1);
//Draw rectangles
CGMutablePathRef path2 =CGPathCreateMutable();//添加矩形的句柄
CGRect rectangle1 = CGRectMake(30, 30, 20, 30);
CGRect rectangle2 = CGRectMake(40, 10, 90, 50);
CGRect rectangles[2] ={rectangle1,rectangle2};
CGPathAddRects(path2, NULL, (const CGRect *)&rectangles,2);
CGContextRef currentContext3 =UIGraphicsGetCurrentContext();
CGContextAddPath(currentContext3, path2);
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];
[[UIColorblackColor] setStroke];
CGContextSetLineWidth(currentContext3, 3);
CGContextDrawPath(currentContext3,kCGPathFillStroke);
CGPathRelease(path2);
//Add Shape Shadow
[selfdrawRectAtTopOfScreen];
[selfdrawRectAtBottomOfScreen];
//绘制线性渐的(Axial轴向)变颜色
CGContextRef currentContext_1 = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext_1);
CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();//给一个RGB色彩空间
UIColor *startColor = [UIColororangeColor];
CGFloat *startColorComponents =(CGFloat *)CGColorGetComponents([startColorCGColor]);
UIColor *endColor = [UIColorblueColor];
CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColorCGColor]);
CGFloat colorComponents[8] = //颜色分量
{
startColorComponents[0],/* Four components of the orange color (RGBA) */
startColorComponents[1],
startColorComponents[2],
startColorComponents[3], /* First color = orange */
endColorComponents[0],/* Four components of the blue color (RGBA) */
endColorComponents[1],
endColorComponents[2],
endColorComponents[3], /* Second color = blue */
};
CGFloat colorIndices[2] =//颜色数组中各个颜色的位置,一种颜色到另一种颜色的变速有多快。必须和CGGradientCreateWithColorComponents的第四个参数个数相同
{
0.0f,/* Color 0 in the colorComponents array */
1.0f,/* Color 1 in the colorComponents array */
};
CGGradientRef gradient = CGGradientCreateWithColorComponents (colorSpace, (constCGFloat *)&colorComponents, (constCGFloat *)&colorIndices, 2);//创建渐变对象句柄
CGColorSpaceRelease(colorSpace);
CGPoint startPoint, endPoint;
startPoint = CGPointMake(120,260);
endPoint = CGPointMake(200.0f,220);
// CGContextDrawLinearGradient (currentContext, gradient, startPoint, endPoint,kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextDrawLinearGradient (currentContext_1, gradient, startPoint, endPoint,0);//0为不扩展渐变
CGGradientRelease(gradient);
CGContextRestoreGState(currentContext_1);
//CGAffineTransformMakeTranslate 仿射变换
CGMutablePathRef pathA=CGPathCreateMutable();/* Create the pathA first. Just the path handle. */
CGRect rectangleA =CGRectMake(10.0f,10.0f, 100.0f,90.0f);
CGAffineTransform transform =CGAffineTransformMakeTranslation(150.0f,0.0f);
CGPathAddRect(pathA, &transform, rectangleA);/* Add the rectangleA to the path */
CGContextRef currentContextB =UIGraphicsGetCurrentContext();
CGContextAddPath(currentContextB, pathA);
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];
[[UIColorbrownColor]setStroke];/* Set the stroke color to brown */
CGContextSetLineWidth(currentContextB, 4.0f);
CGContextDrawPath(currentContextB,kCGPathFillStroke);/* Stroke and fill the path on the context */
CGPathRelease(pathA);
//CGContextTranslateCTM (CTM current transformation matrix当前变换矩阵)
CGMutablePathRef pathCTM =CGPathCreateMutable();
CGRect rectangleCTM =CGRectMake(200,10, 50, 100);
CGPathAddRect(pathCTM, NULL, rectangleCTM);
CGContextRef currentContextCTM =UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContextCTM);
CGContextTranslateCTM(currentContextCTM,.0f, 100.0f);/* Translate the current transformation matrix to the right by 100 points */
CGContextAddPath(currentContextCTM, pathCTM);/* Add the path to the context */
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];
[[UIColorbrownColor] setStroke];
CGContextSetLineWidth(currentContextCTM, 5.0f);
CGContextDrawPath(currentContextCTM,kCGPathFillStroke);
CGPathRelease(pathCTM);
CGContextRestoreGState(currentContextCTM);
CGMutablePathRef pathMC =CGPathCreateMutable();//CGAffineTransformMakeScale 缩放图形
CGRect rectangleMC =CGRectMake(10.0f,300.0f, 200.0f, 300.0f);/* Here are our rectangle boundaries */
CGPathAddRect(pathMC,NULL, rectangleMC);/* Add the rectangle to the path */
CGContextRef currentContextMC =UIGraphicsGetCurrentContext();/* Get the handle to the current context */
CGContextScaleCTM(currentContextMC,0.5f, 0.5f); /* Scale everything drawn on the current graphics context to half itssize */
CGContextAddPath(currentContextMC, pathMC);/* Add the path to the context */
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80alpha:1.0f]setFill]; /* Set the fill color to cornflower blue */
[[UIColorbrownColor]setStroke];/* Set the stroke color to brown */
CGContextSetLineWidth(currentContextMC,5.0f); /* Set the line width (for the stroke) to 5 */
CGContextDrawPath(currentContextMC,kCGPathFillStroke); /* Stroke and fill the path on the context */
CGPathRelease(pathMC); /* Dispose of the path */
//CGAffineTransformMakeRotation 旋转图形 旋转值必为弧度度,正值代表顺时针旋转,负值代表逆时针旋转
CGMutablePathRef pathMR =CGPathCreateMutable();
CGAffineTransform transformMR =CGAffineTransformMakeRotation((45.0f *M_PI) / 180.0f);
CGRect rectangleMR =CGRectMake(200,50, 50, 100);
CGPathAddRect(pathMR, &transformMR, rectangleMR);
CGContextRef currentContextMR =UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContextMR);
CGContextTranslateCTM(currentContextMR, .0f, 100.0f);
CGContextAddPath(currentContextMR, pathMR);/* Add the path to the context */
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];
[[UIColorbrownColor] setStroke];
CGContextSetLineWidth(currentContextMR, 5.0f);
CGContextDrawPath(currentContextMR,kCGPathFillStroke);
CGPathRelease(pathMR);
CGContextRestoreGState(currentContextMR);
}
- (void) drawRectAtTopOfScreen{
CGContextRef currentContext =UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);//保存图形上下文的状态
CGContextSetShadowWithColor(currentContext,CGSizeMake(10.0f,10.0f)/*偏移量*/,20.0f/*模糊度*/, [[UIColorgrayColor] CGColor]);
CGMutablePathRef path =CGPathCreateMutable();
CGRect firstRect = CGRectMake(55.0f, 200.0f, 100.0f, 100.0f);
CGPathAddRect(path, NULL, firstRect);
CGContextAddPath(currentContext, path);
[[UIColorcolorWithRed:0.20fgreen:0.60fblue:0.80falpha:1.0f]setFill];
CGContextDrawPath(currentContext,kCGPathFill);
CGPathRelease(path);
CGContextRestoreGState(currentContext ); //恢复到以前的状态
}
- (void) drawRectAtBottomOfScreen{
/* Get the handle to the current context */
CGContextRef currentContext =UIGraphicsGetCurrentContext();
CGMutablePathRef secondPath = CGPathCreateMutable();
CGRect secondRect = CGRectMake(150.0f, 250.0f, 200.0f, 200.0f);
CGPathAddRect(secondPath, NULL, secondRect);
CGContextAddPath(currentContext, secondPath);
[[UIColorpurpleColor] setFill];
CGContextDrawPath(currentContext,kCGPathFill);
CGPathRelease(secondPath);
}
@end