iOS advanced--Image memory management

Source: Internet
Author: User

Recent work on optimizing the performance of the program involves the image memory management aspect, so the content of this aspect summarizes,

1, why do picture memory management?

Problem A, when the program starts, the memory keeps growing when you browse the picture.

Problem B, when browsing the picture, the program UI is not fluent, even if the local map is not fluent.


2, how to do picture memory management?

Manages image memory within an application domain, pointing to the memory address of the same picture when multiple places in the program need to refer to a picture.


3, how to do?

If the picture is a local resource, the address is fixed, there is no expiration problem we can use the system [UIImage ImageName:] method directly. This method itself is done with the memory cache.

When multiple places call the same picture address, it returns the same memory address.

If it is a picture of the network, we can put the network request image in the cache, the next time we visit, the first check whether the picture is in the cache, if so, then directly return the picture, if not, then check the request again.

As far as the practice is, the URL of the request and the picture in the dictionary, the URL to do key, the image content to do value, if the next time the use of the picture, the first check the URL key is not already exist.

Of course we just said the approximate principle, ignoring many details, memory size management, image file cache management, picture synchronization/asynchronous loading, image memory cleanup, different picture cleanup strategies, and many other issues.

Of course, open source image memory management projects more, to the above problems, the solution is also better, the following is my recommended library

A, Sgimagecache Https://github.com/seatgeek/SGImageCache

[Sgimagecache getimageforurl:url thendo:^ (UIImage *image) {    if (image) {        self.imageView.image = image;    }}];


B, Sdwebimagemanager

Sdwebimagemanager *manager = [Sdwebimagemanager Sharedmanager]; UIImage *cachedimage = [Manager imagewithurl:url];if (cachedimage) {    //Use the cached image immediatly}else{    //S Tart an async download    [manager Downloadwithurl:url delegate:self];}
(Sdwebimagemanager *) Imagemanager didfinishwithimage: (UIImage *) image{    //Do something with the downloaded image} </code>


Reprint Please indicate the source http://blog.csdn.net/sea918 thank you.

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.