Sdwebimage Source Code Analysis

Source: Internet
Author: User

1. Overview

Sdwebimage is a third-party open Source library that is widely used in iOS development, providing some of the columns of images from loading, parsing, processing, caching, cleaning, and so on, so that we can focus on the business process. This article will be from Sdwebimage source code, to step by step analysis, let us more familiar with and understand it, in order to achieve better application purposes.

The most common method of Sdwebimage is the following, which we have used most of the time in the actual development process:

Sd_setimagewithurl: [Nsurl urlwithstring:@ "url"placeholderimage:[UIImage imagenamed: @" Placeholder.png "]];

The design of Sdwebimage is extremely elegant and concise, and the main function is such a line of code, where the complex implementation details are hidden behind this line of code.

The Sdwebimage is the official class diagram:

Through the analysis of the above image, we can divide the Sdwebimage class into three types: view classification; tool class; Core class.

1.1 View Categories

This section contains the following main classes:

    • UIButton(WebCache):UIButtonthe method that loads the picture for the class. For example, under normal circumstances, click on the Image property and background image.
    • MKAnnotationView(WebCache):MKAnnotationViewadd various ways to load pictures for your class.
    • UIImageView(WebCache):Adds a method for loading a picture to a UIImageView class.
    • UIImageView(HighlightedWebCache):UIImageViewadds a way to load a picture in a highlighted state for a class.
    • FLAnimatedImageView(WebCache):FLAnimatedImageViewadd a dynamic method to the class that needs to be introduced into the FLAnimatedImage framework. SDWebImageIt is recommended to use this framework to handle the loading of a dynamic picture (GIF).
    • UIView(WebCache):上面几个Classification, will call the method of this classification sd_internalSetImageWithURL to do picture loading requests, specifically through SDWebImageManager the call to achieve. It also implements operation cancellation, activityindicator addition and cancellation. It also UIView+WebCacheOperation manages the cancellation and recording of requests through classification.
1.2 Tool Classes
    • NSData+ImageContentType: Gets the type of picture according to the image data, such as GIF, PNG, etc.
    • SDWebImageCompat: This profile is primarily compatible with other Apple devices, including many macro definitions and a way to modify the image size based on key.
    • SDImageCacheConfig: Used to manage cache configuration information. such as whether to decode, whether to allow icloud, whether to allow memory cache, cache time and so on. The default cache time is one week.
    • UIImage+MultiFormat: Getting the data for the UIImage object, or generating the uiimage in the specified format based on data, is actually a conversion between uiimage and NSData.
    • UIImage+GIF: For a picture whether GIF makes a judgment. You can return a GIF UIImage object based on NSData and return only the GIF generated by the first picture of GIF. If you want to display more than one GIF, use the FLAnimatedImageView .
    • SDWebImageDecoder: According to the picture, do the decoding processing of the picture, and decide how to deal with the decoding according to the picture's situation.
1.3 Core Class
  • SDImageCache: Responsible for the whole cache work of Sdwebimage, is a singleton 对象 . Cache path processing, cache name processing, managing memory cache and disk cache creation and deletion, obtaining pictures based on specified key, type processing of stored pictures, deleting caches based on cache creation and modification date.
  • SDWebImageManager: This class is 一个单例。 a SDWebImageCache SDWebImageDownloader cache and load processing that has a property and attributes that are used for the picture respectively. Provides a unified interface for loading pictures for UIView and its subclasses. Manages the collection of loading operations, as well as the handling of various load options.
  • SDWebImageDownloader: implementation of the image loading specific processing, if the picture in the cache is loaded from the buffer. If the cache does not exist, create a direct one. Also manages the encapsulation, caching, and cookie setting of the Nsurlrequest object request header. Functions such as the handling of loading options. Manage the dependencies between operation.
  • SDWebImageDownloaderOperation: A custom parallel operation subclass. This class mainly realizes the specific operation of the picture downloading, as well as the picture decoding after the picture downloading completes, Operation life cycle management and so on.
  • FLAnimatedImageView: The data of the dynamic picture is encapsulated by the Alanimatedimage object. FLAnimatedImageViewis UIImageView the child class. It can fully realize the loading display and management of dynamic pictures. And to UIImageView do the process optimization.
2. Implementation process

Sdwebimage for us to achieve the image loading, data processing, image caching and other columns of work. Let's take a look at the process of the relevant operation through the official diagram:

By, we know that the process of loading the sdwebimage is to first load the data from the cache, the cache load is first loaded from the memory cache, and then the disk is loaded. If there is no cache, it is loaded from the network. After the network has successfully loaded the picture, it is cached locally.

3.SDWebImage Source Code Interpretation
    1. Sdwebimage Tool class;
    2. The Sdwebimagecompat of Sdwebimage;
    3. The Sdwebimagemanager of Sdwebimage;
    4. The Sdimagecache of Sdwebimage;
    5. The Sdwebimagedownloader of Sdwebimage;
    6. The sdwebimagedownloaderoperation of Sdwebimage;
    7. The Uiview+webcache of Sdwebimage.
4. Summary

Through the Sdwebimage Open Source Library Learning, there are a few of the following points let me gain more:

    • Decoupling idea : in our actual project development, it is often easy to ignore the importance of decoupling, resulting in more and more code volume, also more and more chaotic, increase the cost of code maintenance, but also indirectly reduce the maintainability of the code. In Sdwebimage, the core class is Sdwebimagemanager, and in this class, it splits two important responsibilities into Sdimagecache and sdwebimagedownloader, so that As the core class Sdwebimagemanager, the functions and ideas as well as the maintenance of the code are relatively much easier.
    • Clear function Flow : From the picture download to the picture display to the view, the Sdwebimage process is very clear. First load the data from the cache, and the cache load takes precedence from the memory cache, and then the disk is loaded. Finally, the cache is not loaded from the network if it is not. Once the network has successfully loaded the picture, it is cached locally.
    • Excellent performance detail handling : Improve user performance by using GCD and nsoperation;
    • Simple API: The category allows us to do things as simple as using the native API of the view.

Sdwebimage Source Code Analysis

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.