iOS 划线圆形 CGContext

http://my.oschina.net/LangZiAiFer/blog/175623

在iPhone屏幕上画长方形,直线和文字


CGContextRef ctx = UIGraphicsGetCurrentContext();

// 设置颜色,仅填充 4 条边
CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] CGColor]);
// 设置线宽为
CGContextSetLineWidth(ctx, 1.0);
// 设置长方形 4 个顶点
CGPoint poins[] = {CGPointMake(5, 5),CGPointMake(425, 5),CGPointMake(425, 125),CGPointMake(5, 125)};
CGContextAddLines(ctx,poins,4);
CGContextClosePath(ctx);

CGContextStrokePath(ctx);




首先,获取上下文

CGContextRef context = UIGraphicsGetCurrentContext();


画无框矩形

  1. //设置矩形填充颜色:红色  
  2. CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
  3. //填充矩形  
  4. CGContextFillRect(context, rect);  
  5. //执行绘画  
  6. CGContextStrokePath(context);  
 

画有框矩形

  1. //设置矩形填充颜色:红色  
  2. CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
  3. //填充矩形  
  4. CGContextFillRect(context, rect);  
  5. //设置画笔颜色:黑色  
  6. CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);  
  7. //设置画笔线条粗细  
  8. CGContextSetLineWidth(context, 1.0);  
  9. //画矩形边框  
  10. CGContextAddRect(context,rect);  
  11. //执行绘画  
  12. CGContextStrokePath(context);  

 

画文字

  1. //设置画笔线条粗细  
  2. CGContextSetLineWidth(context, 1.0);  
  3. //设置矩形填充颜色:红色  
  4. CGContextSetRGBFillColor (context, 1.0, 0.0, 0.0, 1.0);  
  5. //设置字体  
  6. UIFont *font = [UIFont boldSystemFontOfSize:31.0];  
  7. //在指定的矩形区域内画文字  
  8. [text drawInRect:rect withFont:font];  

 

画线

  1. //设置画笔线条粗细  
  2. CGContextSetLineWidth(context, 5.0);  
  3. //设置线条样式  
  4. CGContextSetLineCap(context, kCGLineCapButt);  
  5. //设置画笔颜色:黑色  
  6. CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);  
  7. //画点连线  
  8. CGContextAddLines(context, points, count);  
  9. //执行绘画  
  10. CGContextStrokePath(context);  

ios 在bitmap上绘制文字  

2012-07-10 18:05:31|  分类: iphone 笔记|举报|字号 订阅

  • CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh)
  • {
  •     CGContextRef    context = NULL;
  •     CGColorSpaceRef colorSpace;
  •     void *          bitmapData;
  •     int             bitmapByteCount;
  •     int             bitmapBytesPerRow;

  •     bitmapBytesPerRow   = (pixelsWide * 4);
  •     bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
  •     colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
  •     bitmapData = calloc( bitmapByteCount );
  •     if (bitmapData == NULL)
  •     {
  •         fprintf (stderr, "Memory not allocated!");
  •         return NULL;
  •     }
  •     context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);

  •     if (context== NULL)
  •     {
  •         free (bitmapData);
  •         fprintf (stderr, "Context not created!");
  •         return NULL;
  •     }
  •     CGColorSpaceRelease( colorSpace );
  •     return context;
  • }


    1. CGRect myBoundingBox;
    2.     myBoundingBox = CGRectMake (0, 0, myWidth, myHeight);
    3.     myBitmapContext = MyCreateBitmapContext (400, 300);

    4.     // ********** Your drawing code here ********** 
    5.     CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
    6.     CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 200, 100 ));
    7.     CGContextSetRGBFillColor (myBitmapContext, 0, 0, 1, .5);
    8.     CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 100, 200 ));
    9.     myImage = CGBitmapContextCreateImage (myBitmapContext);
    10.     CGContextDrawImage(myContext, myBoundingBox, myImage);
    11.     char *bitmapData = CGBitmapContextGetData(myBitmapContext); 
    12.     CGContextRelease (myBitmapContext);
    13.     if (bitmapData) free(bitmapData); 
    14.     CGImageRelease(myImage);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值