IOS network-in layman's (i)

Source: Internet
Author: User

First, we take the most commonly used uiimageview as an example to introduce the implementation principle:

1) uiimageview+webcache:setimagewithurl:placeholderimage:options: Show Placeholderimage First, and Sdwebimagemanager by URL To find a picture locally.

2) SDWebImageManager:downloadWithURL:delegate:options:userInfo:SDWebImageManager is the uiimageview+ Webcache classes linked with Sdimagecache, SDImageCache:queryDiskCacheForKey:delegate:userInfo: Used to find images from the cache based on CacheKey is already in the cache

3) If there is already a picture cache in memory, Sdwebimagemanager will callback SDImageCacheDelegate:imageCache:didFindImage:forKey:userInfo:

4) and Uiimageview+webcache callback SDWebImageManagerDelegate:webImageManager:didFinishWithImage: To display the picture.

5) If there is no picture cache in memory, then the build nsinvocationoperation is added to the queue, from the hard disk to find whether the picture has been downloaded cache.

6) Try to read the picture file according to the Urlkey in the hard disk cache directory. This step is performed at Nsoperation, so the callback notifydelegate is returned to the main thread:.

7) If the previous action reads a picture from the hard disk, the picture is added to the in-memory cache (if the free memory is too small, the memory cache is emptied first). Sdimagecachedelegate callback ImageCache:didFindImage:forKey:userInfo:. And then callback the display image.

8) If the picture is not read from the hard disk cache directory, the picture is not present in all caches, the image needs to be downloaded, callback ImageCache:didNotFindImageForKey:userInfo:.

9) share or regenerate a downloader sdwebimagedownloader start downloading pictures.

10) Image download by nsurlconnection to do, to achieve the relevant delegate to determine the picture download, download complete and download failed.

One) Connection:didreceivedata: In the use of ImageIO made by the picture download progress loading effect.

Connectiondidfinishloading: After the data download is finished, give sdwebimagedecoder to do the image decoding processing.

13) Image decoding processing is done in one nsoperationqueue and does not slow down the main thread UI. If there is a need to download the image two times, it is best to complete here, the efficiency will be much better.

14) in the main thread Notifydelegateonmainthreadwithinfo: announce the completion of decoding, ImageDecoder:didFinishDecodingImage:userInfo: callback to Sdwebimagedownloader.

Imagedownloader:didfinishwithimage: Callback to Sdwebimagemanager to inform the picture download completed.

16) Notify all downloaddelegates download complete, callback to the desired place to display pictures.

17) Save the picture to Sdimagecache, the memory cache and the hard disk cache are saved simultaneously.

18) Write the file to the hard disk in a separate nsinvocationoperation to avoid slowing down the main thread.

19) If you are running on iOS, Sdimagecache will register notification to uiapplicationdidreceivememorywarningnotification when initializing and Uiapplicationwillterminatenotification, in memory warning, clean up the memory image cache, the end of the application to clean up the expired image.

Sdwebimageprefetcher can pre-download images for easy follow-up use.

* Above is reproduced *

* The following are original *

Sdwebimage is mainly used for the acquisition of network images.

Of course, it can also be used for other operations.

For example, the operation of the button, but it is very similar.

So there's not much nagging here.

The point is to understand how sdwebimage is implemented and how to use it.

viewcontroller.m//cx-sdwebimage////Created by Ma C on 16/3/24.//Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "uiimageview+webcache.h"//due to network problems the local server static nsstring is used here * URLString = @ "Http://localhost/nvshen.jpeg"; @interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageview; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];}           -(Ibaction) DownLoad: (ID) Sender {/* option all options://Retry after failure (often used) sdwebimageretryfailed = 1 << 0, Download begins during UI interaction, resulting in delayed downloads such as Uiscrollview deceleration.          (often used) sdwebimagelowpriority = 1 << 1,//memory cache only sdwebimagecachememoryonly = 1 << 2,  This flag can be incrementally downloaded and the image displayed is progressively downloaded sdwebimageprogressivedownload = 1 << 3,//Refresh Cache sdwebimagerefreshcached = 1 << 4,//Background Download Sdwebimagecontinueinbackground = 1 << 5,//Priority download Sdwebimagehighpriori      ty = 1 << 8,    Delay placeholder Sdwebimagedelayplaceholder = 1 << 9,//Change animation image sdwebimagetransformanimatedimage = 1 < < 10, */* Just get the picture 1) self.imageview Sd_setimagewithurl:[nsurl urlwithstring:urlstring] get the picture done Operable 2) [Self.imageview Sd_setimagewithurl:[nsurl urlwithstring:urlstring] completed:^ (UIImage *image, NSError *error, S     Dimagecachetype CacheType, Nsurl *imageurl) {}]; Get Animation (Picture Group) 3) Self.imageview sd_setanimationimageswithurls:<# (Nsarray *) #> get 4 from previous cache) Self.imageview Sd_se timagewithpreviouscachedimagewithurl:<# (Nsurl *) #> placeholderimage:<# (UIImage *) #> options:<# ( sdwebimageoptions) #> progress:<#^ (Nsinteger receivedsize, Nsinteger expectedsize) progressBlock#> completed : <#^ (UIImage *image, Nserror *error, Sdimagecachetype cachetype, Nsurl *imageurl) completedblock#> *///More complete  Action/* URl: Here is the network address placeholderimage: Take the picture and show options before the download is complete: Action options completed: After completion   */[Self.imageview sd_setimagewithpreviouscachedimagewithurl:[nsurl urlwithstring:urlstring] PlaceholderImage:nil o Ptions:sdwebimageretryfailed progress:^ (Nsinteger receivedsize, Nsinteger expectedsize) {//Here is the download progress//receiv Edsize already downloaded size//expectedsize total size} completed:^ (UIImage *image, Nserror *error, Sdimagecachetype CacheType, NSUR    L *imageurl) {//download completed//Can be a success, or it can be a failure.    }]; } @end

Sometimes we need to test whether the picture is actually stored in the sandbox, so we need to get to the address of the sandbox.

Methods are as follows

    NSString * Path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);    NSLog (@ "%@", Path);

Once we get the address, we can open it at the terminal

?? Gif??

IOS network-in layman's (i)

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.