I am developing an iOS app which uses quite a few images. I am kind of confused about how to load the images in the app. A similar question was asked around 5 years ago. But a lot has changed since then. So I thought, it would make more sense to start a new thread.
There are mainly two options that I think I have:
Use PaintCode app (you can find it here) which gives you CG code to draw images at the runtime.
Put .png image files (1x, 2x, 3x)
The things like about the first option are:
• The first most important and unbeatable feature: Draw dynamic images i.e. to be able to change the content and gives basic animation effects using variables and equations
• The second most important thing: parametric images that behaves perfectly when frame changes no stretching or distortion
• Less size - As we not gonna use image files, reduces the overall size of the app drastically (top concern these days). E.g. PaintCode App in itself weights only ~5 MB (leaving out icon files).
• Resolution independence
• Very easy to use. Especially you don't need to remember names. You simply make some function calls.
My major concerns are:
- Does drawing images at the runtime affects performance and other critical parameters.
- Under what circumstances, the png files would be a better fit.
So, here I am, seeking opinions from more knowledgable people. Thanks in advance.
If the drawing is simple - use Core Graphics.
You are correct on all the points you make in favour of the Core Graphics/PaintCode approach.
As you say, It's future proof and so much more flexible than importing assets.
You want to change the color of that custom button? Input a different argument into your function, instead of re-making 3 images for each resolution.
You want to scale that drawing up? Just re-draw it with a new size.
The new iPhone 8s comes out sometime in the near future with a 4x display? Your app can already handle it.
As you also say, it decreases the size of your app bundle, which is amazing.
However.
Drawing with Core Graphics is 100% done on the CPU, so it will affect performance if you do numerous & complex drawings on the main thread.
As Tricertops says, a prime example of a complex CG drawing is shadows with large radii. Another example is using complicated paths in your context.
Do not despair though, there are ways to combat this!
You can do drawing off-screen on a background thread, and then pass the drawn image to the screen on the main thread (you must do UI updates from the main thread), but this will still cause some latency problems (images just appearing on the screen from nowhere). However, this can be a good way of loading in complex drawings that you're going to be using at a later stage of the app.
You could also sacrifice some app launch time by loading in your Core Graphics images when the app loads, but this may be undesirable for the user, depending on the complexity and number of images you need to draw.
You can re-use drawings by keeping a cache of images that you're drawing frequently (button backgrounds etc.) This will improve performance by sacrificing a little bit of RAM. PaintCode actually automatically does this when you use non-parameterised StyleKits.
Although, seeing as iOS hardware is much quicker than what it was when this question was asked, the actual CPU usage that drawing Core Graphics code is much less than it used to be, so you can now get away with more complicated drawings on faster devices.
However, you may find that some complex drawings are simply impossible or would take forever to create in Core Graphics. In this case, I would definitely recommend using images.
As the answerer very eloquently put on the post you linked to:
If you're finding yourself spending weeks trying to create a drawing in Core Graphics, you're probably better off just using images and spending that time more productively!
Although, as long as you keep your drawing simple, and re-use drawings wherever you can, you shouldn't have to worry about performance issues with using Core Graphics.
In my opinion, the pros of using Core Graphics/PaintCode for simple drawings far outweighs the drawbacks, so long as it is used properly.