IOS network analysis-(third-party SDWebImage-master for Six-network image acquisition ),
Network image retrieval is used by most apps. Because the original api is rarely used in actual app development, we will not introduce it here. We will introduce it later. This article will introduce you to a third-party-SDWebImage-master. SDWebImage-master is a very powerful third-party. Today we will talk about some main functions. Other functions can be downloaded and pondered by yourself.
* Tip: SDWebImage-master can be downloaded from github. *
When you need to apply SDWeb, put the SDWebImage folder in the folder into the project.
Enter the header file # import "UIImageView + WebCache. h" in the file that requires obtaining images through the network to use the corresponding api.
First, the most basic method is to display (only obtain images)
Code:
/// ViewController. m // CX-SDWebImage-master /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "# import" UIImageView + WebCache. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSString * urlString = [NSString stringWithFormat: @" http: // localhost/tupian.jpg "]; NSURL * url = [NSURL URLWithString: urlString]; UIImageView * imageView = [[UIImageView alloc] init]; imageView. frame = CGRectMake (0, 20, CGRectGetWidth (self. view. frame), 300); [imageView sd_setImageWithURL: url]; [self. view addSubview: imageView];} @ end
In many cases, Images cannot be downloaded due to the network connection, which may result in white space, which may be a poor app experience. Therefore, to solve this problem, this third party also has a placeholder image (this image is easy to understand when you download it .)
Code:
/// ViewController. m // CX-SDWebImage-master /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "# import" UIImageView + WebCache. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSString * urlString = [NSString stringWithFormat: @" http: // localhost/tupian.jpg "]; NSURL * url = [NSURL URLWithString: urlString]; UIImageView * imageView = [[UIImageView alloc] init]; imageView. frame = CGRectMake (0, 20, CGRectGetWidth (self. view. frame), 300); [imageView sd_setImageWithURL: url placeholderImage: [UIImage imageNamed: @ "image name"]; [self. view addSubview: imageView];} @ end
Sometimes (I don't know why there are so many times) It takes some operations to download the image, so block is a good choice.
Code:
/// ViewController. m // CX-SDWebImage-master /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "# import" UIImageView + WebCache. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSString * urlString = [NSString stringWithFormat: @" http: // localhost/tupian.jpg "]; NSURL * url = [NSURL URLWithString: urlString]; UIImageView * imageView = [[UIImageView alloc] init]; imageView. frame = CGRectMake (0, 20, CGRectGetWidth (self. view. frame), 300); [imageView sd_setImageWithURL: url completed: ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, NSURL * imageURL) {NSLog (@ "% @", image); // enumerate NSLog (@ "% ld", (long) cacheType); NSLog (@ "% @", imageURL) ;}]; [self. view addSubview: imageView];}/* result 13:12:18. 091 CX-SDWebImage-master [4626: 283982] <UIImage: 0x7fe8a3f0d900>, {250,250} 13:12:18. 092 CX-SDWebImage-master [4626: 283982] 1 13:12:18. 092 CX-SDWebImage-master [4626: 283982] http: // localhost/tupian.jpg */@ end
Then...
Most of the time we view images, that is, when downloading images, there are