本文介绍了图像与核心图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS设备上,使用Core Graphics绘制图形比使用图像文件更好吗?

On iOS devices, in which occasions is it better to draw graphics using Core Graphics than using image files?

这样做在资源方面有什么好处? ?

What are the advantages of doing so in terms of resources?

推荐答案

图像与核心图形是一个直截了当的区别。渲染屏幕外/屏幕图形的方法更加复杂,以至于您需要使用Instruments来查找实际发生的情况。我试着在这里提供一个概述,但是这个答案可以使用更多知识渊博的人的一些改进。

Images vs Core Graphics is a blunt distinction. The methods to render offscreen/onscreen graphics are more complex to the point that you need to use Instruments to find out what is really happening. I tried to provide an overview here, but this answer could use some improving from more knowledgeable people.

GPU总是在屏幕上呈现图形。但是,它们可以由GPU或CPU生成,并且发生在用户代码或称为渲染服务器的单独进程中。以下是概述:

Graphics are always rendered onscreen by the GPU. However, they can be generated by the GPU or the CPU, and happen in user code or in a separate process called "the render server". Here is an overview:

CPU,用户代码:


  • 核心图形和核心文本

  • drawRect()。结果通常是缓存的。

  • Core Graphics and Core Text
  • drawRect(). The result is usually cached.

GPU,渲染服务器:

GPU, render server:


  • CALayer的 shouldRasterize 设置为YES。这会创建图层和子图层的缓存。

  • CALayer with a shouldRasterize set to YES. This creates a cache of the layer and sublayers.

GPU,渲染服务器,非常慢:

GPU, render server, very slow:


  • CALayer使用蒙版( setMasksToBounds )和动态阴影( setShadow * )。

  • 组不透明度( UIViewGroupOpacity )。

  • CALayer using masks (setMasksToBounds) and dynamic shadows (setShadow*).
  • Group opacity (UIViewGroupOpacity).

GPU,快速:


  • 创建来自PNG文件的图像。可拉伸图像中的拉伸也仅是GPU。

请注意,缓存仅在重用缓存时才有用。如果立即丢弃它会损害性能。例如,可以缓存和重用内容被简单拉伸的缓存动画,但内容更改的缓存动画将具有可怕的性能。

Note that caching is only useful if the cache is reused. If it is immediately discarded it hurts performance. For example, a cached animation where contents are simply stretched can be cached and reused, but a cached animation where contents change will have an awful performance.

图像文件通常更快。


  • 可以使用。

  • 可以在后台从磁盘读取和解压缩图像。

  • 图像可以缓存在内存中如果你使用 imageNamed:而不是 initWithData:

  • Image files can be downloaded to disk in advance using aggressive caching.
  • Images can be read and decompressed from disk on the background.
  • Images can be cached in memory if you use imageNamed: instead initWithData:.

离线绘图需要更多工作,但可以让你获得更多。

Offscreen drawing requires more work, but lets you achieve more.


  • 你可以使用复杂图形制作动画没有质量损失,因为图形在每一帧都被重写。

  • 您可以随时使用i18n创建图形。

  • 如果您不需要,您应该禁用隐式Core Graphics动画。示例:带有圆角的UIView(您只需要舍入,而不是动画)。

  • 绘图可能非常复杂,您需要使用Instruments来查看时间的去向。

  • 除非使用遮罩,阴影,边缘抗锯齿或组不透明度,否则可能会缓存使用drawRect进行绘制。您可以请求缓存调用 - [CALayer setShouldRasterize:YES] - [CALayer setRasterizationScale:]

  • You can animate complex graphics with no quality loss, because the graphic is rewriten on every frame.
  • You can create graphics with i18n on the fly.
  • You should disable the implicit Core Graphics animation if you don't need it. Example: UIView with round corners (you just need the rounding, not the animation).
  • Drawing can be complex enough that you need to use Instruments to see where the time is going.
  • Drawing with drawRect is probably cached unless you use masking, shadows, edge antialiasing, or group opacity. You can request caching calling -[CALayer setShouldRasterize:YES] and -[CALayer setRasterizationScale:].

可拉伸图像,无论是从图像文件读取还是通过绘图生成,都使用更少的内存。拉伸是对GPU的一种低成本操作。

Stretchable images, whether read from image files, or generated by drawing, use less memory. Stretching is an unexpensive operation to the GPU.

如果没有足够的性能,性能只是一个问题。除非另有说明,否则请使用速度更快的代码。最快的计划是首先进入市场的计划。

Performance is only a problem if there isn't enough. Use whatever is faster to code unless pressed otherwise. The fastest program is the one that reaches the market first.

一些有趣的阅读:


  • 。不要错过Andy Matuschak的评论。我用本文的大量内容编辑了原始答案。

  • >应用程序框架>

  • >要点>

  • >要点>

  • >要点>

  • >要点> 。

  • Designing for iOS: Graphics & Performance. Don't miss the comments from Andy Matuschak. I edited the original answer with a lot of content from this article.
  • WWDC 2011 > App Frameworks > Session 121: Understanding UIKit Rendering
  • WWDC 2012 > Essentials > Session 238: iOS App Performance: Graphics and Animations
  • WWDC 2012 > Essentials > Session 211: Building Concurrent User Interfaces on iOS
  • WWDC 2012 > Essentials > Session 223: Enhancing User Experience with Scroll Views
  • WWDC 2012 > Essentials > Session 240: Polishing Your Interface Rotations.

这篇关于图像与核心图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:00