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?
The Core Graphics framework is based on the Quartz advanced drawing engine. It provides low-level, lightweight 2D rendering with unmatched output fidelity.
Core Image is an image processing and analysis technology that provides high-performance processing for still and video images. Use the many built-in image filters to process images and build complex effects by chaining filters. For details, see Core Image Filter Reference.
Does Core Graphics Use Gpu? A CPU is used for rendering in Core Graphics, and graphics cards are GPU-based in Core Animation.
Swift Core Graphics. Harness the power of Quartz technology to perform lightweight 2D rendering with high-fidelity output. Handle path-based drawing, anti-aliased rendering, gradients, images, color management, PDF documents, and more.
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.
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, user code:
drawRect()
. The result is usually cached.GPU, render server:
shouldRasterize
set to YES. This creates a cache of the layer and sublayers.GPU, render server, very slow:
setMasksToBounds
) and dynamic shadows (setShadow*
).UIViewGroupOpacity
). GPU, fast:
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.
Image files are generally faster.
imageNamed:
instead initWithData:
.Offscreen drawing requires more work, but lets you achieve more.
-[CALayer setShouldRasterize:YES]
and -[CALayer setRasterizationScale:]
.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.
Some interesting reading:
In my experience it's always better to use images from a performance point of view, but sometimes you need to draw things manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With