2013-04-10 130 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),只有一個黑色像素到最左邊,後面是與下一行距離相同數量的白色像素。