什么是使它成为核心图形不落后的正确方法? [英] What is the proper way to make it so core graphics does not lag?

查看:208
本文介绍了什么是使它成为核心图形不落后的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UIBezierPath进行手指绘画(我的应用)。我使用path = [UIBezierPath bezier path]创建它; 。它经常滞后在iPad上(从内部调用它并没有改变任何东西)。我已经连续数小时研究这个问题并且没有找到解决方案,只是滞后。请问有人帮我吗?另外,我使用NSTimer来调用该函数。这是我的应用程序工作的旧方式,所以请帮我修复这个问题!!!!!

I use UIBezierPath for finger painting (my app). I create it using path = [UIBezierPath bezier path]; . It constantly lags on the iPad (and calling it from within drawrect did not change anything). I have been working on this for hours on end and have found no solution, just lag. Would somebody be so kind to help me please? Also, I am using a NSTimer to call the function. That is the old way my app will work so please help me fix this lagggg!!!!!

推荐答案

既然没有你的问题包含了足够多的细节,我将做一些不正确的事情并发布一个元答案你的 当前 最后 问题

Since none of your questions contains enough details on its own, I'm going to do something improper and post a meta-answer to your current last five questions.

首先,中绘制drawRect:而在其他任何地方。

First, draw in drawRect: and nowhere else.

这是非常重要的,我将再次说出来。

That's so important that I'm going to say it again.

以<$ c绘制$ c> drawRect:以及其他任何地方。

Draw in drawRect: and nowhere else.

不是 touchesMoved:withEvent:

不是 touchesBegan:已结束:

drawRect:以及其他任何地方。

如果您要将图像保存到文件,那就是一件事。但是当你画到屏幕上时,除了 drawRect: 之外,你不会做任何事。

If you're making an image to save to a file, that's one thing. But when you're drawing to the screen, you don't do it anywhere other than drawRect:.

说真的。这很重要。

第2步:不要试图强制在任何其他时间进行绘图。

drawRect:在您抽出时为您调用。根据定义,在任何其他时间,您不需要绘制,因此绘图是在做您不需要做的事情。通过扩展,不要调用 drawRect:你自己。你不需要也从不帮助。

drawRect: is called for you when it's time for you to draw. By definition, at any other time, you don't need to draw, so drawing is doing things you don't need to do. By extension, don't call drawRect: yourself. You don't need to and it never helps.

实际上,这只是步骤1的扩展。如果你打电话给 drawRect:,如果您有接到电话的绘图代码,则没有什么不同。绘图代码不会在任何地方重复,这很好,但它仍然在错误的时间运行。 允许 drawRect:仅在系统调用时调用。

Actually, that's just an extension of step 1. If you call drawRect:, it's no different from if you had the drawing code where you have the call. The drawing code isn't repeated everywhere, which is nice, but it's still running at the wrong time. Let drawRect: only be called when the system calls it.

setNeedsDisplay 存在告诉系统是时候绘制了。你应该这样做,时,你需要绘制的东西已经改变了。您的视图的属性,模型中的某些内容 - 每当 您将绘制的内容发生更改时,请自行发送 setNeedsDisplay 。不要在任何其他时间做;你不需要。

setNeedsDisplay exists to tell the system that it's time to draw. You should do this when, and only when, something has changed that you'll need to draw. Your view's properties, something in the model—whenever what you will draw changes, send yourself setNeedsDisplay. Don't do it at any other time; you don't need to.

剪掉计时器。你不需要它。无论如何,已经有一个计时器,限制你到60 fps。

Cut out the timer. You don't need it. There's already a timer in place anyway, limiting you to 60 fps.

核心图形不会滞后。不,真的,它没有。缓慢或滞后是因为你要么做得太多,要么你做错了。

Core Graphics does not lag. No, really, it doesn't. Slowness or "lag" is because either you're trying to do too much or you're doing something wrong.

不要不必要地缓存。你正在做的事情都不需要图像缓存。

Don't cache unnecessarily. Nothing you're doing requires an image cache.

滞后是因为你试图从绘制或强制绘图touchesMoved:withEvent:和/或 touchesBegan: / 已结束:。见上文。

The "lag" is because you're trying to draw, or to force drawing, from touchesMoved:withEvent: and/or touchesBegan:/Ended:. See above.

这是你需要做的:

在你的 touchesBegan: / 已移动: / 已结束:方法或其他适当的响应方法,更新您的状态。这将包括Bézier路径。 不要绘制,包括不要调用 drawRect: 或以其他方式强制绘图。

In your touchesBegan:/Moved:/Ended: methods, or other responder methods as appropriate, update your state. This will include the Bézier path. Do not draw, and that includes do not call drawRect: or otherwise attempt to force drawing.

在您更新州后,只有在您完成此操作后,请自行发送 setNeedsDisplay

After you've updated your state, and only if you've done so, send yourself setNeedsDisplay.

drawRect:方法中,以及 drawRect中的 方法,绘制路径,渐变等等,无论你认为合适。

In your drawRect: method, and only in your drawRect: method, draw the path, gradient, whatever, however you see fit.

做这些事情,你的应用程序会很快。使用核心图形。没有滞后。

Do these things, and your application will be fast. With Core Graphics. Without lag.

此外,还有一些重要的读物:

Also, some important reading:

  • View Programming Guide for iOS
  • Drawing … Programming Guide for iOS
  • Quartz 2D (Core Graphics) Programming Guide
  • Performance Overview (some parts are Mac OS X specific, but much of it is relevant on iOS as well)
  • Instruments User Guide

这篇关于什么是使它成为核心图形不落后的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆