最简单的画线功能

如何在iOS屏幕上画出一条线来?这是一切复杂画线的基础。之前在 计算一个点是否在一个区域中 中已经有使用CGPath的示例。那是用来标定区域的,这里使用CGPath画线。

image

示例非常简单。

首先要有个UIImageView,在本例中声明为成员变量:

@interface PathDemoViewController : UIViewController { 
    UIImageView *imageView; 
}

画线的代码:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    
    imageView=[[UIImageView alloc] initWithFrame:self.view.frame]; 
    [self.view addSubview:imageView]; 
    
    self.view.backgroundColor=[UIColor blueColor]; 
    
    UIGraphicsBeginImageContext(imageView.frame.size); 
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); 
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100, 100); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200, 100); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    imageView.image=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
}

其中:

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

设置了线的边缘样式:

image

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值