2014-01-29 3 views
1

iPad에서 "그리기 응용 프로그램"을 그리면 엄청난 양의 메모리가 사용되어 결국에는 충돌이 일어납니다 (또는 최소한 선을 그릴 때는 매우 지루함). "Drawing apps"에 대한 예제를 많이 보았습니다. 나는 그 중 2 개가 효과가 있다는 것을 알았지 만, 그 둘을 그리려 할 때 모두 뒤떨어져있었습니다.Core Graphics 드로잉 코드가 느리게 실행되는 이유는 무엇입니까?

), 나는 아이폰/아이 패드 시뮬레이터를 사용하는 경우입니다 원활립니다,하지만 내 아이 패드 2 세대에 많이 :(이 사람을 도움이 될 것입니다 경우

내 응용 프로그램의 진짜 목적은,는 "서명 응용 프로그램"입니다

를 지연

앱 그리기에 대한 경험이 있거나 (코드가 잘못된 부분을 볼 수있는 경우) 대답을 남겨주세요. 그것은 인정 될 것이다!

내 코드는 (또한 스크린 샷) 아래에 나열되어

:

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

Instruments 또는 다른 도구를 사용하여 코드를 프로파일 링하고 성능을 테스트 해 보셨습니까? 우리는 단순히 당신에게 대답을 줄 것을 기대할 수 없습니다. – SevenBits

답변

1

문제는 아마 모든 touch 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를 사용하여 경로를 구축하십시오. –

관련 문제