2017年3月3日 | Leave a comment 问题: ios core graphics 如何画图 解决: 例1: Objective-C NSString *fontName = @""; NSUInteger count1 = arc4random() % ([UIFont familyNames].count); NSString *familyName = [UIFont familyNames][count1]; NSUInteger count2 = [UIFont fontNamesForFamilyName:familyName].count; fontName = [UIFont fontNamesForFamilyName:familyName][arc4random() % count2]; UIFont *font = [UIFont fontWithName:fontName size:30.0f]; NSString *string = @"Core Graphics"; [string drawAtPoint:CGPointMake(30.0f, 100.0f) withFont:font]; 12345678910 NSString *fontName = @""; NSUInteger count1 = arc4random() % ([UIFont familyNames].count); NSString *familyName = [UIFont familyNames][count1]; NSUInteger count2 = [UIFont fontNamesForFamilyName:familyName].count; fontName = [UIFont fontNamesForFamilyName:familyName][arc4random() % count2]; UIFont *font = [UIFont fontWithName:fontName size:30.0f]; NSString *string = @"Core Graphics"; [string drawAtPoint:CGPointMake(30.0f, 100.0f) withFont:font]; 例2: Objective-C - (void)drawSomething{ CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文 CGMutablePathRef path = CGPathCreateMutable();//创建路径 CGPathMoveToPoint(path, nil, 20, 50);//移动到指定位置(设置路径起点) CGPathAddLineToPoint(path, nil, 20, 100);//绘制直线(从起始位置开始) CGContextAddPath(context, path);//把路径添加到上下文(画布)中 //设置图形上下文状态属性 CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1);//设置笔触颜色 CGContextSetRGBFillColor(context, 0, 1.0, 0, 1);//设置填充色 CGContextSetLineWidth(context, 2.0);//设置线条宽度 CGContextSetLineCap(context, kCGLineCapRound);//设置顶点样式 CGContextSetLineJoin(context, kCGLineJoinRound);//设置连接点样式 CGFloat lengths[2] = { 18, 9 }; CGContextSetLineDash(context, 0, lengths, 2); CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 0, [UIColor blackColor].CGColor); CGContextDrawPath(context, kCGPathFillStroke);//最后一个参数是填充类型 } 123456789101112131415161718 - (void)drawSomething{ CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文 CGMutablePathRef path = CGPathCreateMutable();//创建路径 CGPathMoveToPoint(path, nil, 20, 50);//移动到指定位置(设置路径起点) CGPathAddLineToPoint(path, nil, 20, 100);//绘制直线(从起始位置开始) CGContextAddPath(context, path);//把路径添加到上下文(画布)中 //设置图形上下文状态属性 CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1);//设置笔触颜色 CGContextSetRGBFillColor(context, 0, 1.0, 0, 1);//设置填充色 CGContextSetLineWidth(context, 2.0);//设置线条宽度 CGContextSetLineCap(context, kCGLineCapRound);//设置顶点样式 CGContextSetLineJoin(context, kCGLineJoinRound);//设置连接点样式 CGFloat lengths[2] = { 18, 9 }; CGContextSetLineDash(context, 0, lengths, 2); CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 0, [UIColor blackColor].CGColor); CGContextDrawPath(context, kCGPathFillStroke);//最后一个参数是填充类型} 例3: //DrawingRect CGRect strokeRect = CGRectMake(25, 85, 263, 60); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 2.0f); CGContextStrokeRect(context, strokeRect); 12345 //DrawingRect CGRect strokeRect = CGRectMake(25, 85, 263, 60); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 2.0f); CGContextStrokeRect(context, strokeRect); //DrawingRect CGRect strokeRect = CGRectMake(25, 85, 263, 60); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 2.0f); CGContextStrokeRect(context, strokeRect); 12345 //DrawingRect CGRect strokeRect = CGRectMake(25, 85, 263, 60); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetLineWidth(context, 2.0f); CGContextStrokeRect(context, strokeRect); //DrawingGradient CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)[UIColor colorWithRed:0.3 green:0.0 blue:0.0 alpha:0.2].CGColor, (__bridge id)[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.8].CGColor]; const CGFloat locations[] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); CGPoint startPoint = CGPointMake(CGRectGetMinX(strokeRect), CGRectGetMinY(strokeRect)); //矩形最小x,y CGPoint endPoint = CGPointMake(CGRectGetMaxX(strokeRect), CGRectGetMaxY(strokeRect)); //矩形最大x,y CGContextSaveGState(context); CGContextAddRect(context, strokeRect); CGContextClip(context); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); //开始绘制 CGContextRestoreGState(context); //释放资源 CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace); 1234567891011121314151617181920 //DrawingGradient CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSArray *colors = @[(__bridge id)[UIColor colorWithRed:0.3 green:0.0 blue:0.0 alpha:0.2].CGColor, (__bridge id)[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.8].CGColor]; const CGFloat locations[] = {0.0, 1.0}; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); CGPoint startPoint = CGPointMake(CGRectGetMinX(strokeRect), CGRectGetMinY(strokeRect)); //矩形最小x,y CGPoint endPoint = CGPointMake(CGRectGetMaxX(strokeRect), CGRectGetMaxY(strokeRect)); //矩形最大x,y CGContextSaveGState(context); CGContextAddRect(context, strokeRect); CGContextClip(context); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); //开始绘制 CGContextRestoreGState(context); //释放资源 CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace); 参考: http://www.mamicode.com/info-detail-841887.html http://blog.csdn.net/cocoarannie/article/details/9990411 https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001066