b2cloud

27th June 2012

The benefits of using CoreGraphics instead of images

Tutorial By 4 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:

CGContextMoveToPoint(context,5,0);
CGContextAddLineToPoint(context,0,10);
CGContextAddLineToPoint(context,10,10);
CGContextAddLineToPoint(context,5,0);
CGContextSetRGBFillColor(context,1,1,1,1);
CGContextFillPath(context);

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.

Recommended Posts

iOS performSelector with multiple parameters

Post by 4 years ago

On iOS the built in convenience method performSelector method call only allows for up to 2 parameters. - (void) aMethod { [self performSelector:@selector(doSomethingWithObject:otherObject:) withObject:@(1) withObject:@(2)]; } - (void) doSomethingWithObject:(id) object otherObject:(id) otherObject { // Code

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!