1
我有一個應用程序允許用戶繪製圖像,以便最終將其裁剪到繪製的路徑。不幸的是,它很慢。原因是它依靠[self setNeedsDisplay]
爲了刷新UIBezierPath,這也導致圖像重繪,並阻礙性能。有沒有一種方法可以實現這一點,而不必在每次調用setNeedsDisplay
時重新繪製UIImage?或者更好的方式來實現整個事情?所有幫助表示讚賞!下面是我的UIView子類:UIView子類重繪UIBezierPath而不重繪整個視圖
#import "DrawView.h"
@implementation DrawView
{
UIBezierPath *path;
}
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO]; // (2)
[self setBackgroundColor:[UIColor whiteColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void)drawRect:(CGRect)rect // (5)
{
[self.EditImage drawInRect:self.bounds];
[[UIColor blackColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
@end
工作正常!謝謝!!!! –