iphone CGContextRef 使用画…

本文介绍了如何使用 iOS 中的 CGContextRef 进行简单的图形绘制,包括正方形填充与描边、文字绘制及直线画法等基本操作。还提供了从 UIImageView 中获取 CGContextRef 的方法。

获取上下文

CGContextRef context = UIGraphicsGetCurrentContext();

 

画一个正方形图形 没有边框

CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

 

写文字

CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);

UIFont  *font = [UIFont boldSystemFontOfSize:11.0];

[@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];

 

画一条线

CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 200,20);
CGContextStrokePath(context);

 

画正方形边框

CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0); 
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

//////////////////转自:http://yangping-account.iteye.com/blog/1116960///////////////////////


从UIImageView中获取CGContextRef:

(从别人那转的,没用过)

        UIGraphicsBeginImageContextWithOptions(newImageSize, YES, 0);

        CGContextRef context = UIGraphicsGetCurrentContext();

        // drawing commands go here

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值