如何使用物镜C逐像素绘制一条线 [英] How to Draw a line pixel by pixel using objective C

查看:55
本文介绍了如何使用物镜C逐像素绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone上画一条线,这样我就可以查看正在绘制的每个像素。



我目前正在使用计时器调用drawRect方法。 / p>

以下是我使用数组中的值绘制波浪的代码:

  static int i = 0; 
int x = 10;
int y = 0;
int x2;
int y2;

-(void)drawRect:(CGRect)rect
{
//绘制代码。
CGContextRef c = UIGraphicsGetCurrentContext();

CGFloat black [4] = {0.0f,0.0f,0.0f,1.0f};
CGContextSetStrokeColor(c,黑色);
CGContextBeginPath(c);
NSLog(@ fired ...);
int ecg [358] = {36,37,37,36,34,33,33,....,36}; //假设数据在那里

CGContextMoveToPoint(c,x,y);

x2 = i;
y2 = ecg [i];
CGContextAddLineToPoint(c,x2,y2);
x = x2;
y = y2;

CGContextStrokePath(c);
[self setNeedsDisplay];
i ++;
}

-(void)awakeFromNib
{
NSTimer * timer;
timer = [NSTimercheduledTimerWithTimeInterval:0.05 target:self选择器:@selector(drawRect :) userInfo:nil重复:是];
}

调用drawRect时,一次只能看到一行。但是我看不到整个图像。我想不出其他方法才能看到波浪在屏幕上绘制。



此外,永远不会调用方法initWithFrame,因此必须与awakeFromNib一起声明计时器,如果我错了,请纠正我。



任何动画化线条绘制的指针都会有所帮助。



谢谢

解决方案

只需重新绘制内容即可,例如:

  for(int j = 0; j< = i; j ++){
x2 = j;
y2 = ecg [j];
CGContextAddLineToPoint(c,x2,y2);
x = x2;
y = y2;
}

CGContextStrokePath(c);

请勿在您的<中调用 setNeedsDisplay: code> drawRect:,它会导致视图一遍又一遍地重画。另外,请尝试避免使用全局变量。例如,这里不需要缓存 x,x2,y和y2。在 drawRect:中定义它们。


I want to draw a line(on iphone) such that i can view each pixel being draw.

I am currently calling the drawRect method using a timer.

Following is the code where i am drawing a wave using the values from an array:

static int i=0;
int x = 10;
int y = 0;
int x2;
int y2;

- (void)drawRect:(CGRect)rect 
{
// Drawing code.
CGContextRef c = UIGraphicsGetCurrentContext();

CGFloat black[4] = {0.0f,0.0f,0.0f,1.0f};
CGContextSetStrokeColor(c, black);
CGContextBeginPath(c);
NSLog(@"fired...");
int ecg[358] = {36, 37, 37, 36, 34, 33, 33, .... ,36}; //Assume the data is there

CGContextMoveToPoint(c, x, y);

x2 = i;
y2 = ecg[i];
CGContextAddLineToPoint(c, x2, y2);
x = x2;
y = y2;

CGContextStrokePath(c); 
[self setNeedsDisplay];
i++;
}

- (void) awakeFromNib
{
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawRect:) userInfo:nil repeats:YES];
}

I can only see one line at a time being draw when drawRect is called. But i dont see the entire image. I cannot think of a different approach in order to see the wave being draw on the screen.

Also the method initWithFrame is never called so had to go with awakeFromNib to declare the timer, correct me if i am wrong.

Any pointers to animate the drawing of a line would help.

Thank you

解决方案

Just re-draw the stuff up to the point that you want, like:

    for (int j = 0; j <= i; j++) {
        x2 = j;
        y2 = ecg[j];
        CGContextAddLineToPoint(c, x2, y2);
        x = x2;
        y = y2;
    }

    CGContextStrokePath(c); 

Do not call setNeedsDisplay: in your drawRect:, it causes the view to be redrawn immediately afterwards over and over again. Also, try to avoid global variables. For examples, there's no need to "cache" x, x2, y and y2 here. Define them inside drawRect:.

这篇关于如何使用物镜C逐像素绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆