Basic Use of SDWebImage Framework for iOS Development Network Chapter 1

SDWebImage Links: http://www.jianshu.com/p/be9a0a088feb

SDWebImage project file.




Some internal details in the SD Web Image framework:

// When a memory warning occurs
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    // 1. Empty the cache
    // clearDisk: Delete it directly and recreate it
    // CleaDisk: Clean the expired cache, calculate the size of the current cache, and compare it with the maximum number of caches set, if exceeded\
        Continue deleting (in order of file creation)
    // Outdated cache: 7 days
    [[SDWebImageManager sharedManager].imageCache cleanDisk];
    
    // 2. Cancel all current operations
    [[SDWebImageManager sharedManager] cancelAll];
    
    // Internal details of SDWebImage framework
    
    // 3. Maximum concurrency: 6. MaxConcurrent Downloads is an attribute in SDB Image Downloader. H
    
    // 4. How to deal with the saved name of the cached file? We use the content framework to get the last/last of the URL path. We use the content framework to get the URL path and encrypt the path with MD5.
    
    // 5. How does this framework handle internal memory warnings? Internally, the cache is cleared by listening for notifications (in SDIMAGE Cache. m)
    
    // 6. The framework for caching: We use: Variable Dictionary Framework: NSCache
    
    // 7. How to judge the type of picture: When judging the type of picture, only match the first byte (judging by the binary format of the picture) (in NSData + Image ContentType. m)
    
    // 8. Task handling in queues: FIFO (an enumeration of task handling is defined in SDB Image Downloader.h file)
    
    // 9. How to download pictures? Send network requests to download, NSURLConnection. Continuously splice the data returned to us by the server (252 lines in SDW Image Downloader Operation. m file).
    
    // 10 Network request timeout? 15s (in SDB Image Downloader. m file)

    /*
     SDWebImage Core:
     SDWebImageManager management class: There are two classes below (SDImageCache caching class, SDWebImageDownloader tool class, download)
     The image download task is handled in an SDWebImageDownloaderOperation class under the SDWebImageDownloader class.
     */

Related codes:

//  ViewController.m
//  05 Master - Basic Use of SD WebImage Framework
//
//  Created by Chaoyang on 2017/11/28.
//  Copyright 2017 sunny. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageDownloader.h"
#import "UIImage+GIF.h"
#import "NSData+ImageContentType.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self judgeImageType];
}

//Download pictures and need to get download progress
// Make memory cache-disk cache
- (void)download
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] placeholderImage:[UIImage imageNamed:@"qq"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        switch (cacheType) {
            case SDImageCacheTypeNone:
                NSLog(@"Download pictures");
                break;
            case SDImageCacheTypeDisk:
                NSLog(@"Disk cache");
                break;
            case SDImageCacheTypeMemory:
                NSLog(@"Memory cache");
                break;
                
            default:
                break;
        }
    }];
    NSLog(@"%@",NSHomeDirectory());
    
}

// Simply get a picture, no settings
// Make memory cache-disk cache
- (void)download1
{
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        NSLog(@"%@--UI",[NSThread currentThread]);
        // image is a picture obtained by url
        self.imageView.image = image;
        
    }];
}

// Task-free cache processing
// Cache processing without tasks
- (void)download2
{
    //Data: Picture binary data
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        
//        NSLog (@"%@ - UI", [NSThread current Thread]); refresh UI in sub-thread
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
        
    }];
}

// Play GIF pictures
- (void)playGIF
{
    
    NSURL *url = [NSURL URLWithString:@"http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=3e4079778ad4b31cf0699cbfb2e60b49/c9fcc3cec3fdfc03e6e8818cd53f8794a4c22675.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage sd_animatedGIFWithData:data];
    self.imageView.image = image;

}

// Judging Picture Type
- (void)judgeImageType
{
    NSData *imageData = [NSData dataWithContentsOfFile:@"/Users/sunny/Desktop/photo/head.JPG"];
    NSString *typeStr = [NSData sd_contentTypeForImageData:imageData];
    NSLog(@"%@",typeStr);
}

@end


Keywords: network Attribute

Added by tpearce2 on Sun, 19 May 2019 13:49:38 +0300