2013-04-10 136 views
1

嘿,我需要帮助建立一个应用程序。我构建了一个艺术应用程序,这个应用程序正在与干扰。这就是我需要在这个应用程序中绘制多条线的原因。干扰更多的线条更好。我认为问题在于iPad无法处理太多线条,因为速度或性能太慢。 我不知道如何加快我的代码在iPad上的更多性能。我应该使用Open GL还是其他什么...用性能绘制很多线条

我该怎么办?

这里是Draw.m

#import "Draw.h" 

@implementation Draw 


- (IBAction) sliderValueChanged:(UISlider *)sender { 
    label.text = [NSString stringWithFormat:@"%f", slider.value]; 
    //NSLog(@"slider value = %f", sender.value); 
    [self setNeedsDisplay]; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

    } 
    return self; 
} 




- (void)drawRect:(CGRect)rect 
{ 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    //NSLog(@"slider value = %f", self.bounds.size.width); 

    CGMutablePathRef cgpath = CGPathCreateMutable(); 
    CGPathMoveToPoint(cgpath, NULL, 0, 500); 


    CGMutablePathRef cgpath2 = CGPathCreateMutable(); 
    CGPathMoveToPoint(cgpath2, NULL, 0, 500); 

    UIBezierPath *uipath = [[UIBezierPath alloc] init]; 
    [uipath moveToPoint:CGPointMake(0, 0)]; 

    int step = 5; 
    int iterations = self.bounds.size.width/step; 

    for (int i = 0; i < iterations+1; i++){ 
    //CGPathAddCurveToPoint(cgpath, NULL, 1+i, 0, 1+i, 0, 1+i ,0); 

     CGPathAddLineToPoint (cgpath, NULL, 0, 0); 
     CGPathAddLineToPoint (cgpath, NULL, 0, 768); 
     CGPathAddLineToPoint (cgpath, NULL, step*i-slider.value*2, 768); 
     CGPathAddLineToPoint (cgpath, NULL, step*i, 0); 
     CGPathAddLineToPoint (cgpath, NULL, (step*i)+step, 0); 

    [[UIColor blackColor] setStroke]; 
    CGContextAddPath(ctx, cgpath); 

    [self strokeUIBezierPath:uipath]; 

    CGPathRelease(cgpath); 
} 

- (void)strokeContext:(CGContextRef)context 
{ 
    CGContextStrokePath(context); 
} 

- (void)strokeUIBezierPath:(UIBezierPath*)path 
{ 
    [path stroke]; 
} 

@end 

image http://img17.imageshack.us/img17/375/53178410200339197475308.jpg

+0

你可以请张贴你想要画的一张或两张图片(用不同的滑块值)吗?这将有助于找出更好的方法来绘制它。 – 2013-04-10 21:39:08

+0

此外,您的代码似乎不正确。你永远不会调用'strokeContext(context)',所以CGPath永远不会被执行。然而,你正在调用'strokeUIBezierPath:',其中没有行的Bezier路径,你永远不会修改它。 – 2013-04-10 21:42:07

+0

这里从模拟器的屏幕截图http://img17.imageshack.us/img17/375/53178410200339197475308.jpg 当前的滑块控件的干扰... 该代码是在实验时刻,但我认为这是不是主要问题... – greenchapter 2013-04-10 22:50:53

回答

1

与贝塞尔路径的问题是,它们可以是相当 '计算重'。

您既可以使用直通线(CGContextAddLineToPoint(context, point.x, point.y);

您也可以使用图形加速。 您可以直接跳入OpenGL或使用游戏引擎来帮助您完成一些代码。

最流行的一个(我认为很容易使用)是cocos2d。

+0

我读了一些有关UIBezierPath的东西,它似乎比CGContext更快,但在下一步中,我应该尝试使用OpenGL – greenchapter 2013-04-10 22:53:54

+0

,您认为opengl更适合那样吗? – greenchapter 2013-04-18 21:28:53

+0

看着你张贴的图像,我会建议将一切都绘制成图像,然后显示图像,如果这是可能的话..你也可以缩放图像,如果需要的话。 – 2013-04-21 12:12:30

0

使用图案填充屏幕,您应该会获得更好的性能。整个部分的示例代码为the Quartz Programming Guide

在你的情况下,你可以创建一个非常小的图案单元格(高度= 1),只有一个黑色像素到最左边,后面是与下一行距离相同数量的白色像素。