Sdwebimage cache principle

SDWebImage provides a UIImageView class to support loading remote images from the Internet. With cache management, asynchronous download, the same URL download frequency control and optimization features.

Sdwebimage cache flow chart

Sdwebimage cache principle

Independent asynchronous image download

You may use a separate asynchronous image download, you must use - (id "SDWebImageOperaTIon") downloadImageWithURL: (NSURL *) url

opTIons: (SDWebImageOpTIons) opTIons

Progress:(SDWebImageDownloaderProgressBlock)progressBlock

Completed:(SDWebImageCompletionWithFinishedBlock)completedBlock; to create an instance of SDWebImageDownLoader. In this way, there can be a callback of the download progress and a callback of the download completion, and the operation related to the progress bar and the operation related to displaying the image can be completed in the callback.

Independent asynchronous image cache

The SDImageCache class provides a singleton class that manages the cache.

SDImageCache *imageCache = [SDImageCache sharedImageCache]

The URL is used as the key when finding and caching images. (First look for memory, if the picture does not exist in the memory, then look for the hard disk; when looking for the hard disk, use the MD5 value of the URL as the key).

Find an image:

UIImage *cacheImage = [imageCache mageFromKey:myCacheKey];

Cache image:

[ imageCache storeImage:myImage forKey:myCacheKey];

By default, images are stored in the in-memory cache and disk cache. If you just want to cache into memory, you can use the following method:

storeImage:forKey:toDisk: The third parameter can be passed NO.

Mainly used objects:

1. UIImageView (WebCache), the import package, to achieve the callback after reading the image.

2.SDWebImagemanager, a relay station that manages images, records which images are being read. Read the Cache to the lower layer (call SDImageCache), or read the object to the network (call SDWebImageDownloader). Implement callbacks for SDImageCache and SDWebImageDownLoader.

3. SDImageCache, according to the URL as the key, store and read the image (there is memory (with URL as the key) and the existing hard disk (with the MD5 value of the URL as the key)). Implement image and memory cleanup work.

SDWebImage loading image process

1. The entry setImageWithURL:placeholderImage:options: will display the placeholderImage first, then the SDWebImageManager will start processing the image according to the URL.

2. Enter the downloadWithURL:delegate:options:userInfo: in the SDWebImageManager class, and give it to

SDImageCache finds if the image has been downloaded from the cache

queryDiskCacheForKey:delegate:userInfo:.

3. First find out if there is a picture from the memory picture cache. If there is already a picture cache in the memory, SDImageCacheDelegate callback imageCache:didFindImage:forKey:userInfo:to

SDWebImageManager.

4.SDWebImageManagerDelegate callback

webImageManager:didFinishWithImage: Go to UIImageView+WebCache, and wait for the front end to display the image.

5. If there is no memory cache, generate `NSOperation `

Add to the queue and start looking for images from the hard disk that have been cached.

6. According to the MD5 value of the URL, Key attempts to read the image file in the hard disk cache directory. This step is the operation performed in NSOperation, so go back to the main thread and make a result callback notifyDelegate:.

7. If the previous operation read the picture from the hard disk, add the picture to the memory cache (if the free memory is too small, the memory cache will be cleared first). SDImageCacheDelegate' callback imageCache:didFindImage:forKey:userInfo:`. Then callback to show the picture.

8. If the image is not read from the hard disk cache directory, the image does not exist in all caches. You need to download the image and call back imageCache:didNotFindImageForKey:userInfo:.

9. Share or regenerate a downloader SDWebImageDownloader to start downloading images.

10. Image download is done by NSURLConnection to implement the related delegate

To judge the picture download, download completion and download failed.

11.connection:didReceiveData: Use ImageIO to load the effect according to the picture download progress.

12.connectionDidFinishLoading: After the data is downloaded, it will be sent to SDWebImageDecoder for image decoding.

13. The image decoding process is completed in an NSOperationQueue, which will not slow down the main thread UI. If there is a need to reprocess the downloaded image, it is best to do it here, and the efficiency will be much better.

14. In the main thread notifyDelegateOnMainThreadWithInfo:

Declare decoding completion imageDecoder:didFinishDecodingImage:userInfo: Callback to SDWebImageDownloader`.

15.imageDownloader:didFinishWithImage: The callback tells SDWebImageManager that the image download is complete.

-16. Notify all downloadDelegates that the download is complete, and callback to show the image where it is needed.

17. Save the image to SDImageCache, and save the memory cache and hard disk cache at the same time. Writing files to the hard disk is also done in a separate NSOperation, avoiding slowing down the main thread.

18.SDImageCache will register some message notifications during initialization.

Clean up the memory image cache when the memory warns or retreats to the background, and clean up the expired images when the application ends.

Marine Device

The ship power plant is the power equipment set up to ensure the normal operation of the ship. It provides various energy for the ship and uses the energy to ensure the normal navigation of the ship, the normal life of the personnel, and the completion of various operations. Ship power plant is all the machinery and equipment for the generation, transmission and consumption of various energy, and it is an important part of the ship. Ship power plant includes three main parts: main power plant, auxiliary power plant, other auxiliary machinery and equipment.

marine device,Manual hydraulic valve remote control device

Taizhou Jiabo Instrument Technology Co., Ltd. , https://www.jbcbyq.com

Posted on