gpt4 book ai didi

iphone - 使用 Core Graphics 绘制图形线

转载 作者:行者123 更新时间:2023-11-28 22:52:42 24 4
gpt4 key购买 nike

我正在尝试绘制一个显示在 imageview 上的随机图形,我尝试使用这段代码

-(void)createGraph{
UIGraphicsBeginImageContext(self.drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
int i = 0;
while (i<=20) {
int r = rand() % 100;

CGContextMoveToPoint(context, 20, 320);
CGContextAddLineToPoint(context, linePoint.x+20+i*r, linePoint.y+320-i*r);
CGContextStrokePath(context);
i++;

NSLog(@"random value %d",r);
}
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
}

但是它在单条线上画了一条对角线。已显示在下图中

我期待绘制一条图形线!使用核心图形 enter image description here

最佳答案

首先,您不能将CGContextAddLineToPoint 放入循环中。删除(仅)循环,并在例如中进行另一个循环viewDidLoad,在每次迭代时,调用 -(void)createGraph 给出最后一个点和下一个点。现在,您将分配给 CGContextMoveToPoint 的最后一个点,您将分配给 CGContextAddLineToPoint 的下一个点。顺便说一下,在 CGContextSetLineWidth(context, 2.0); 之后添加 CGContextBeginPath(context);

关于iphone - 使用 Core Graphics 绘制图形线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11542655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com