Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS - Best practice to handle large number of images (Performance + Size on disk)

My app UIKit stores something like 100 (100x100) jpeg files, that suppose to be used as "pattern image". the average size for each image is sometimes like 20~40 kb.

I'm cocos2d-x developer as well. In cocos environment i'm using plist to "bound" each image and than cut it via plist. This is great performance and size saver, but as far as I know its not possible on UIKit.

So my question is this, Is there a better way to store/pull this images to improve disk capacity and increase performance, other than drag and drop the files into XCode and use it as usual?

like image 775
Roi Mulia Avatar asked Jul 21 '15 13:07

Roi Mulia


2 Answers

[any number] of images is not really a problem on iOS since there are advanced caching systems in place that take care of reuse of the images. Also the rendering system of iOS is quite great so you don't have to worry about it.

While it is certainly possible to program system for sprites, I would not advice you to do it. This method is primarily used in Web Development (because each image has to be served with new request *note no longer true with HTTP/2) and also obviously for game development (because the less bind texture calls you have, the better).

There is one more good example for why not to use sprites - if you are developing Watch application and you want to do animation, you do it through flipboard style images (sequence of images named 1.png - 100.png), and not by using large atlas of images. While it would be guessing as why did they decide to do it that way (my guess would be because of how good it works internally + throughput of the bluetooth), it is obvious that it is also preferred choice of Apple and so we should follow.

For iOS, there are some pitfalls that you should know:

  • Any image loaded from the Web should not be loaded on main thread, the same really goes with every image that is not present at the load of the screen (images in UITableViewCell are great example, because you can see mini-freezes if the images are large as you are scrolling through)
  • If you have many layers, images with alpha channel != 1 decrease performance heavily (but are often unavoidable)
  • Background image created with UIColor.colorWithPatternImage() should be used with caution, as this method is considered problematic (details here)

Now as for asynchronous loading of the images, I suggest you take a look at following libraries:

  • Haneke (Obj-C, Swift)
  • SDWebImage (Obj-C)

Both of them are great so it is really matter of preference (I like Haneke better), but they allow you to download images on different threads, be it from Web or from your bundle. They also have extensions for UIImageView which allows you to use 1-line function to load all images easily.

Hope it helps!

like image 79
Jiri Trecak Avatar answered Nov 05 '22 23:11

Jiri Trecak


There will be in iOS 9 if that counts ;) It's called On Demand Resources and lets you store content with Apple and download the content when it's needed for a level, etc. It'll be great for game apps and is what they used as an example at WWDC.

Check it out here: https://developer.apple.com/library/prerelease/ios/documentation/FileManagement/Conceptual/On_Demand_Resources_Guide/

like image 23
Drmorgan Avatar answered Nov 05 '22 23:11

Drmorgan