The benefits of using CoreGraphics instead of images

Tutorial By 5 years ago

When I first started iOS development, I found it easy to cut images out of photoshop files to use for buttons and other graphical element, and although there is nothing wrong with this approach in some cases its simply not the best method when considering the download size of your app, and the CPU and memory resources used when the app decompresses the PNG for use in the graphical interface.

As you become more familiar with CoreGraphics, you can see how effective it is to use it to render objects that would otherwise be rendered with simple graphics (such as gradient effects). While this may seem daunting at first, once you go digging you will find out that most of the work has actually been done for you. For instance, imageless gradient buttons can be done using the GradientButton class, simple modification of the code allows you to specify your own colours. UIGlossyButton is another example of imageless buttons. This is not only useful on buttons, it can also be used to draw custom shapes, so if for example you had a “play” button in your app, instead of using a PNG why not just draw it as a triangle:


This code starts at the top of the triangle (the point), and goes to the left side, then the right and closes it back up. Simply change the coordinates and you will have an imageless play button. Obviously once you know how to use coordinates to draw a path you can draw practically any shape you want, the only exception to this being curved shapes, in order to do curves you will have to know how to use Bezier Paths, and that will be the subject of another blog.

To sum up, you should try and not use images where you can in your project, it leads to faster loading times, less memory used and smaller application sizes.