2014-02-20 45 views
0

我創建了一個繪圖應用程序來繪製當您觸摸屏幕,它在模擬器中運行非常流暢,但是當我在iPad 2上測試它時,速度變慢並且需要很長時間才能繪製我正在使用這些代碼:繪圖應用程序性能

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    lastPoint = [touch locationInView:self.view]; 
} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    currentPoint = [touch locationInView:self.view]; 



    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:self.view.frame]; 

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 7.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 


    [drawImage setFrame:self.view.frame]; 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    lastPoint = currentPoint; 

    [self.view addSubview:drawImage]; 
} 

我認爲這個問題是調用drawInRect:方法touchMoved:方法

+0

嘗試使用Time Profiler工具分析此問題 - 它會告訴您哪些方法調用的處理時間最長。查看「Core Animation Essentials」WWDC視頻:https://developer.apple.com/videos/wwdc/2011/index.php – elimirks

+0

這似乎是使用displayLink的好用的應用程序。無論發生多少次觸摸事件,每秒只能調用setNeedsDisplay這麼多次。 – Putz1103

+0

是的,我知道在drawInRect:調用的錯誤,因爲當我刪除它工作順利,但繪圖並不完美在這種情況下 – user3330895

回答

1
  1. 建立了UIBezierPath在touchesMoved,並沒有做任何圖紙存在。
  2. 在touchesMoved中調用setNeedsDisplay。
  3. 重寫drawRect並在那裏繪製路徑。

OR

使用爲您的視圖到CAShapeLayer層後盾,點添加到touchesMoved層。

+0

你能解釋一下嗎?代碼 – user3330895

+0

我不明白你是什麼意思? – user3330895

+0

+1這是正確的答案。 @ user3330895不要指望任何人爲你寫代碼;你會自己做更多的事情來學習。斯蒂芬的回答非常明確 - 如果你不明白,你應該:a)閱讀你應該如何在視圖中繪畫,b)具體詢問你不明白的東西。 – Caleb