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

标签 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

最佳答案

大部分性能问题可能是由于在每次触摸事件时绘制之前的图像。 [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

我要尝试的第一件事是创建一个从 UIView 派生的 View ,并在 drawRect 中完成所有绘制。这样你就不会因为在每个触摸事件上绘图而阻塞。当您响应触摸事件时,您需要在自定义 View 上调用 setNeedsDisplay。

我要尝试的第二件事是使用 CAShapeLayer 作为自定义 View 上的图层支持。当您这样做时,您不需要重写 drawRect。您只需要为层提供点。

关于ios - 为什么我的 Core Graphics 绘图代码运行缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441055/

相关文章:

iphone - UITableviewCell 取消选择回调

ios - 如何使用 subview 调整 UIView 的大小

ios - Swift 2.1 - 错误处理 - 未编译多个 Catch

ios - NSNull 类型的 JSON 值 '<null>' 无法转换为 NSString

ios - Segue 后 UILabel 不更新

ios - UITableViewDelegate 在 iOS 中是否有类似 tableViewWillEndDisplayingCell 的方法?

javascript - 基于对象的 Canvas javascript 事件

javascript - 我需要在页面刷新时记住我的 Canvas 绘图

ios - 在 URL Scheme OpenURL 之后从 AppDelegate 调用 ViewController 方法

optimization - 如何使用 SFML 更快地渲染 400 多个多边形