iOS picture cache

Source: Internet
Author: User

iOS memory management is always a big problem for developers, when memory consumption is too large, it is easy to be killed by the system, developers need to optimize the memory consumption problem as much as possible.

Now the app interface to do more and more refined, the integration of a large number of images, the first thing I think about is how to reduce the memory of these images.

Usually we load the picture using the following way: 1.imageNamed: After the image loading, the system will cache it in memory, when the second load, directly from the memory read, the advantage is the loading speed, the disadvantage is that will always occupy the system memory, when our images need to load repeatedly, This is a good choice; 2.imageWithContentOfFile: images are loaded only once, not cached in memory, and then the memory is freed up and reloaded each time, this loading method is suitable for loading only a few times.

Asset Catalog is a new Image resource management tool introduced by Apple, which is easy and intuitive to package. Apple will package the picture resources into. Car files, we can not get inside the picture path, can not be loaded using imagewithcontentoffile way, can only use imagenamed to load, it means that the loaded images are all slowly in memory, so that the entire app occupies the internal Saved will be relatively large, the system kill the risk of the big.

Workaround: Since the previous project has been using asset catalog to manage the image resources, it is now necessary to find ways to reduce the memory footprint of the entire app. The first way to think of it is to move the picture out of the asset catalog. Copy all the contents of the asset catalog and delete the asset cataglog the entire directory.

If it is the original use asset catalog correct words, @1x, @2x, @3x material add correct words, so do directly OK, is no problem. If there is improper use of the trouble, such as the problem of improper naming, the direct reading will cause problems, we need to manually parse and load.

Go to Asset Catalog: echo "{" assets\ ": [" > A.json find images.xcassets/-name *.json | while read jsonfile; Do tmppath=${jsonfile%.imageset/ } assetname=${tmppath## /} echo "{\" assetname\ ": \" ${assetname}\ ", \" content \ ":" >> A.json cat $jsonfile >> A.json; Echo '}, ' >>a.json done Echo ']} ' >>a.json

With the last comma removed, A.json becomes a standard JSON file, and all we need to do is parse the file and load the different files according to the actual situation.

Use category to extend the UIImage class, overriding the Imagenamed method, so that all the original load with the cache has become a way without caching, of course, this needs to be flexible processing, which pictures need to be loaded after the cache in memory, which do not need to cache in memory, should be processed separately.

iOS picture cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.