iOS - which is the better option to put images? Core Graphics(PaintCode App) vs Image files(png)

后端 未结 1 1059
走了就别回头了
走了就别回头了 2020-12-31 08:59

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

1条回答
  •  礼貌的吻别
    2020-12-31 09:43

    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.

    • That's less downloading for the user and less worries about size optimisation on your part.

    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:

    The fastest program is the one that reaches the market first.

    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.

    0 讨论(0)
提交回复
热议问题