IOS network analysis-(third-party SDWebImage-master for Six-network image acquisition ),

Source: Internet
Author: User

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

Related Article

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.