IOS network-simple introduction (I)-& gt; third-party SDWebImage and iossdwebimage

Source: Internet
Author: User

IOS network-simple introduction (I)-> third-party SDWebImage and iossdwebimage

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

1) UIImageView + WebCache: setImageWithURL: placeholderImage: options: displays placeholderImage first, and SDWebImageManager searches for images locally based on the URL.

2) SDWebImageManager: downloadWithURL: delegate: options: userInfo: SDWebImageManager is a class linking UIImageView + WebCache with SDImageCache, SDImageCache: queryDiskCacheForKey: delegate: userInfo: used to query from the cache whether the image is already in the cache based on CacheKey

3) if the image cache already exists in the memory, SDWebImageManager calls back SDImageCacheDelegate: imageCache: didFindImage: forKey: userInfo:

4) UIImageView + WebCache calls back SDWebImageManagerDelegate: webImageManager: didFinishWithImage: to display images.

5) if there is no image cache in the memory, NSInvocationOperation is generated and added to the queue to check whether the image has been downloaded from the hard disk.

6) Try to read image files in the hard disk cache directory based on the URLKey. This step is performed in NSOperation. Therefore, the main thread returns the result callback policydelegate :.

7) if the image is read from the hard disk in the previous operation, add the image to the memory cache (if the idle memory is too small, the memory cache will be cleared first ). SDImageCacheDelegate callback imageCache: didFindImage: forKey: userInfo :. Then, the callback shows the image.

8) if the image cannot be read from the hard disk cache directory, it indicates that no image exists in the cache. You need to download the image and call back imageCache: didNotFindImageForKey: userInfo :.

9) share or regenerate a download tool SDWebImageDownloader to download the image.

10) NSURLConnection is used to download images. Related delegate is implemented to determine whether the images are being downloaded, completed, and failed to be downloaded.

11) connection: didReceiveData: The ImageIO is used to load the image according to the download progress.

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

13) the image decoding process is completed in an NSOperationQueue without slowing down the main thread UI. If you need to process the downloaded image twice, it is better to do so here, which will be much more efficient.

14) in the main thread yydelegateonmainthreadwithinfo: the decoding is completed, and imageDecoder: didFinishDecodingImage: userInfo: calls back to SDWebImageDownloader.

15) imageDownloader: didFinishWithImage: calls back to SDWebImageManager to notify the image download is complete.

16) Notify all downloadDelegates that the download is complete, and call back to display the image to the desired location.

17) Save the image to SDImageCache, and save the memory cache and hard disk cache at the same time.

18) Writing files to the hard disk is completed in a separate NSInvocationOperation to avoid slowing down the main thread.

19) if it is running on iOS, SDImageCache registers the notification to UIApplicationDidReceiveMemoryWarningNotification and UIApplicationWillTerminateNotification during initialization, clears the memory image Cache during memory warning, and clears expired images at the end of the application.

20) SDWebImagePrefetcher can download images in advance for later use.

* The above is reprinted *

* Original *

 

SDWebImage is mainly used to obtain network images.

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

For example, button operations are similar.

So I am not so nagging here.

The key point is to understand how SDWebImage is implemented and used.

/// ViewController. m // CX-go to 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 * urlString = @" http: // localhost/nvshen.jpeg "is used here; @ interface ViewController () @ property (weak, nonatomic) IBOutlet UIImageView * imageVIew; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(IBAction) downLoad :( id) sender {/* options all options: // after failure Retry (frequently used) SDWebImageRetryFailed = 1 <0, // download started during UI interaction, resulting in delayed download such as UIScrollView slowing down. (Frequently used) SDWebImageLowPriority = 1 <1, // only performs memory cache SDWebImageCacheMemoryOnly = 1 <2, // this flag can be downloaded progressively, the displayed image is gradually downloaded from SDWebImageProgressiveDownload = 1 <3, // refresh the cache SDWebImageRefreshCached = 1 <4, // SDWebImageContinueInBackground = 1 <5, // preferentially download SDWebImageHighPriority = 1 <8, // latency placeholder SDWebImageDelayPlaceholder = 1 <9, // change the animation image SDWebImageTransformAnimatedImage = 1 <10, * // * just get the image 1) self. ima GeVIew sd_setImageWithURL: [NSURL URLWithString: urlString] After obtaining the image, the operation can be performed. 2) [self. imageVIew sd_setImageWithURL: [NSURL URLWithString: urlString] completed: ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, NSURL * imageURL) {}]; get animation (image group) 3) self. imageVIew sd_setAnimationImagesWithURLs: <# (NSArray *) #> obtain from the cache 4) self. imageVIew sd_setImageWithPreviousCachedImageWithURL: <# (NSURL *) #> placeholder Image: <# (UIImage *) #> options: <# (SDWebImageOptions) #> progress: <# ^ (NSInteger receivedSize, NSInteger expectedSize) progressBlock #> completed: <# ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, NSURL * imageURL) completedBlock #> * // complete operation/* URl: Here is the network address placeholderImage: occupied as an image. The options: Operation Option completed:> */[self. imageVIew sd_setImageWithPreviousCachedImageWithURL: [NSURL URLWithS Tring: urlString] placeholderImage: nil options: SDWebImageRetryFailed progress: ^ (NSInteger receivedSize, NSInteger expectedSize) {// here is the download progress // receivedSize the total size of the downloaded file // expectedSize} completed: ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, NSURL * imageURL) {// After the download is complete, // either the download is successful or the download fails. }];} @ End

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

The method is as follows:

    NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSLog(@"%@",path);

After obtaining the address, we can open it on the terminal.

Animated GIF watermark

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.