为什么我的Core Graphics绘图代码运行缓慢?

时间:2014-01-29 19:46:15

标签: ios objective-c drawing cgcontext

当我尝试在我的iPad中绘制“绘制应用程序”时,它会使用大量内存并最终崩溃(或者至少只是画线非常滞后)。我一直在寻找关于“绘图应用程序”的例子。我发现只有2个有效,但是当它们试图吸引它们时,两者都落后了很多。如果我使用iPhone / iPad模拟器绘制顺畅,但在我的iPad 2G上却落后了很多:(

我的应用程序的真正目的是“签名应用程序”,如果它可以帮助任何人;)

如果您有绘制应用程序的任何经验(或者可以看到我的代码出错的地方),请留下答案。我们将不胜感激!!

我的代码列在下面(也是截图):

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 个答案:

答案 0 :(得分:1)

大多数性能问题可能来自于每次触摸事件上绘制上一张图像。 [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

我要尝试的第一件事是创建一个从UIView派生的视图,并在drawRect中完成所有绘图。这样你就不会阻止每一次触摸事件。当您回复触摸事件时,您需要在自定义视图上调用setNeedsDisplay。

我要尝试的第二件事是使用CAShapeLayer作为自定义视图上的图层支持。执行此操作时,您不需要覆盖drawRect。你只需要为图层提供分数。