2014-01-29 70 views
1

當我嘗試在我的iPad中繪製「繪製應用程序」時,它使用了大量的內存並最終崩潰(或者至少只是繪製線條非常緩慢)。我一直在尋找很多關於「繪圖應用程序」的例子。我只找到了2個工作,但試圖吸引他們時,都落後了很多。如果我使用iPhone/iPad的模擬器繪製順利,但在我的iPad的2G它落後了很多:(爲什麼我的Core Graphics繪圖代碼運行緩慢?

我的應用程序的真正目的是「簽名的應用程序」,如果它會幫助任何人;)

如果您有任何繪製應用程序的經驗(或者可以查看我的代碼錯誤),請留下一個答案。將不勝感激!

我的代碼如下(也有截圖)上市:

ViewController.m:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 

[super viewDidLoad]; 
drawImage = [[UIImageView alloc] initWithImage:nil]; 
drawImage.frame = self.view.frame; 
[self.view addSubview:drawImage]; 
self.view.backgroundColor = [UIColor lightGrayColor]; 
mouseMoved = 0; 

} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

mouseSwiped = NO; 
UITouch *touch = [touches anyObject]; 

lastPoint = [touch locationInView:self.view]; 

} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

mouseSwiped = YES; 

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

UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 255.0, 255.0, 255.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

mouseMoved++; 

if (mouseMoved == 10) { 
    mouseMoved = 0; 
} 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

if(!mouseSwiped) { 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 255.0, 255.0, 255.0, 1.0); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
} 


- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
} 

@end 

ViewController.h:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
    CGPoint lastPoint; 
    UIImageView *drawImage; 
    BOOL mouseSwiped; 
    int mouseMoved; 
} 


@end 

iPad screenshot

+1

您是否嘗試過使用儀器或其他工具來分析代碼並測試性能?你不能指望我們只是給你答案。 – SevenBits

回答

1

大多數表現問題可能來自於每次觸摸EV上的前一圖像ENT。 [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

我會嘗試的第一件事就是創建一個從UIView派生的視圖,並在drawRect中執行所有繪製。這樣你就不會因爲每次觸摸事件而被阻塞。當您響應觸摸事件時,您需要在自定義視圖上調用setNeedsDisplay。

我會嘗試的第二件事是使用一個CAShapeLayer作爲您的自定義視圖的支持層。當你這樣做時,你不需要重寫drawRect。你只需要給圖層的點。

+0

我還應該提到,當您重寫DrawRect時,每次只繪製整個路徑,不要使用輔助圖像。使用UIBezierPath或CGPath來建立你的路徑。 –

相關問題