Management and cleanup of sdwebimage asynchronous loading picture and cache

Source: Internet
Author: User

Original address: http://blog.csdn.net/g1jun/article/details/25687061

First, Sdwebimage introduction

1, in the project development process, we often use the function of asynchronously loading pictures, first download pictures from the network asynchronously, and then through the Uiimageview display on the screen. This is a frequently used feature, basically all the networking applications have to use the function, now GitHub has a very mature framework

Sdwebimage (provides asynchronous load picture, cache management, download progress, etc., most complete) egoimageloading (previously used frameworks, cache management is not as simple as sdwebimage design) afnetworking (only provides asynchronous load picture function, No cache management) Here is the main introduction to Sdwebimage, the framework used most, on the GitHub has given some of the framework of the use of (Details Https://github.com/rs/SDWebImage), we directly download this framework, Run inside the demo found Xcode will report the compilation error, this is because sdwebimage in the project referenced GitHub another project LIBWEBP project, if you want to run the demo online has given a solution.
2, we just use the asynchronous loading picture and cache management related functions, do not need LIBWEBP project, new project, the Sdwebimage directory of the source code directly added to the project



second, the use of sdwebimage provided by the class extension implementation of asynchronous loading pictures

1, Sdwebimage provides the Uiimageview extension class, only needs to add the file reference in the header file

[OBJC] view plain copy #import "Uiimageview+webcache.h"


To load picture methods asynchronously:

[OBJC] View plain copy-(void) Setimagewithurl: (nsurl *) URL placeholderimage: (uiimage *) placeholder; URL picture's address placeholder the image displayed when the network picture is not loaded successfully

By default, Sdwebimage ignores the cache settings in the header, saves the picture as a key URL, and the URL is one by one corresponding to the picture. So when you request the same URL, Sdwebimage gets the picture from the cache. In general, this method can meet our application requirements, but if you request the same URL, and this picture is updated on the server side, the local client requests again will return the old picture in the cache, such as loading the Avatar class frequently updated pictures, the avatar can not update the problem, Because the URL and picture one by one corresponds, one solution is to change the partial URL address way to implement the update, but this method is very complicated to operate, the following describes a more convenient way

[OBJC] View plain copy-(void) Setimagewithurl: (nsurl *) URL placeholderimage: (uiimage *) placeholder options: ( sdwebimageoptions) options;
We have set the third parameter to sdwebimagerefreshcached to realize the picture update operation, the use method is very simple, as follows:

[OBJC] view plain copy nsurl *url = [Nsurl urlwithstring:@ "Http://www.example.com/image.png"];      UIImage *defaultimage = [uiimage imagenamed:@ "Default.png"]; [Self.imageview Setimagewithurl:url placeholderimage:defaultimage options:sdwebimagerefreshcached];

third, cache calculation and cleaning

1, cache calculation, the cache calculation here, only calculate sdwebimage cache, the other files in the Sandbox folder are not included:

[OBJC] view plain copy [SDWebImageManager.sharedManager.imageCache culatesizewithcompletionblock:^ ( Nsuinteger FileCount, Nsuinteger totalsize) {}];

2. Cache cleanup

There are two types of caching in the Sdwebimage, one is disk caching, the other is the memory cache, the framework provides the appropriate method:

[OBJC] view plain copy [[[[Sdwebimagemanager Sharedmanager] imagecache] cleardisk]; [[[[Sdwebimagemanager Sharedmanager] imagecache] clearmemory];


However, pay special attention here, in IOS7 you will find that the use of these two methods to cache total cleanup is not clean, even if there is data under the network. This is because the caching mechanism has been modified in IOS7, using both methods to clear only the sdwebimage slow

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.