简体   繁体   English

在iPhone和iPad上,不使用drawRect可以绘制任何东西吗?

[英]On iPhone and iPad, can we draw anything without using drawRect?

It seems that the standard way to draw dots, lines, circles, and Bezier paths is to draw them in inside of drawRect . 似乎绘制点,线,圆和Bezier路径的标准方法是在drawRect内部绘制它们。 We don't directly call drawRect , but just let iOS call it and we can use [self setNeedsDisplay] to tell iOS to try to call drawRect when it can... 我们不直接调用drawRect ,而是让iOS调用它,我们可以使用[self setNeedsDisplay]告诉iOS尝试在可能的时候调用drawRect

It also seems that we cannot rely on 似乎我们也不能依靠

[self setClearsContextBeforeDrawing: NO];

to not clear the background of the view before calling drawRect . 在调用drawRect之前不清除视图的背景。 Some details are in this question: UIView: how to do non-destructive drawing? 这个问题中有一些细节: UIView:如何进行无损绘制?

How about directly drawing on the screen -- without putting those code in drawRect . 如何直接在屏幕上绘图-无需将这些代码放入drawRect For example, in ViewController.m , have some code that directly draw dots, lines, circles on the screen. 例如,在ViewController.m ,有一些代码可以直接在屏幕上绘制点,线,圆。 Is that possible? 那可能吗?

Without having to drop into OpenGL, the closest you can do to get around the erasure is to convert the context as an image using something like CGBitmapContextCreateImage . 无需拖放到OpenGL中,可以解决的最接近的事情就是使用CGBitmapContextCreateImage之类的将上下文转换为图像。 From there, you can retain the image in memory (or write it to disk if necessary), and then when you redraw the view, you first draw this original image into the context and then overlay it with new content. 从那里,您可以将图像保留在内存中(或在必要时将其写入磁盘),然后在重新绘制视图时,首先将原始图像绘制到上下文中,然后将其覆盖新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM