Most third party frameworks are not perfect, but we lack thinking, and we will conduct defect analysis for more third parties later.

SD source analysis resources are many, here is a simple summary of the core, on the basis of the analysis of performance in terms of whether there is further room for operation.

SD has two core tasks: image decoding + caching, both of which are handled on child threads.

Core a decoder: figure, SDWebImageDecoder part of the code, picture decoding operation

Core two cache: as shown, SDImageCache file part of the code, pictures stored in the cache disk operation

Note that SD is stored locally as a PNG or JPG file from the code. You can also find the sD-related cache folder in the sandbox to verify that it is PNG and JPG.

We need to understand a little UIImagePNGRepresentation and UIImageJPEGRepresentation generate PNG and JPG, is to a compression coding of images, if the load out again, is the need to decode.

Another perspective: the PNG or JPG format of image compression corresponds to the ZIP or RAR format of file compression. The zip or RAR format needs to be decoded when the file is used, and the PNG or JPG format needs to be decoded when the image is displayed.

Try the following code for a deeper understanding of image manipulation:

Problem summary:

First, WHEN SD is stored in the local cache, the image is re-coded.

When SD loads an image from the local cache, it needs to decode the image again.

Encoding and decoding are resource-intensive operations that can be optimized at this step.

Optimization scheme:

Further optimization of the local cache to store decoded data can omit the encoding and decoding operations mentioned in the above problem summary.

In the scheme, we need to customize the data structure for storage, data structure includes: picture attribute format + picture data format; Image file attribute data format, such as image width, Heigth, colorSpace, bitsPerComponent, etc. Image data format, such as RGBA pixel data.

Data structures such as: the first 100 bytes are image attribute data, the following bytes are RGBA data, the first 100 bytes are 0 to 4 bytes width of the image, 5 to 8 bytes height of the image, etc.

Pros and cons analysis of the scheme:

1, disadvantages: storage decoded data will take up more disk memory

2, disadvantages: need to customize the data structure for storage, stored file can not see the file image effect like PNG or JPG, localization has no visual effect

3, advantages: omit part of the encoding and decoding operation, performance has been improved, and the custom data structure can be seen as a shallow encryption of the picture, only familiar with your data structure, to decode the picture

The framework commonly used by AFNetwork and MJRefresh is analyzed