SDWebImage of the image download framework of the multi-thread network in iOS,

Source: Internet
Author: User
Tags ssl certificate

SDWebImage of the image download framework of the multi-thread network in iOS,

CAT/CAT sharing, must be excellent

For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents

Effect:

Code:
-(NSArray *) apps {if (! _ Apps) {NSArray * dictArray = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "apps. plist "ofType: nil]; NSMutableArray * appArray = [NSMutableArray array]; for (NSDictionary * dict in dictArray) {HMApp * app = [HMApp appWithDict: dict]; [appArray addObject: app];} _ apps = appArray;} return _ apps;}-(void) viewDidLoad {[super viewDidLoad];} # pragma mark-data source method-(NSInteger) tabl EView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. apps. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ "app"; UITableViewCell * cell = [tableView preview: ID]; if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: ID];} // obtain the data model HMApp * app = self. apps [indexPath. row]; // set the cell title cell. textLabel. text = app. name; // set the number of cell downloads. detailTextLabel. text = app. download; // position image UIImage * place = [UIImage imageNamed: @ "57437179_000089b0"]; // use the SDWebImage framework to solve asynchronous download, cache, Sandbox cache, and other operations in one sentence, and set the image [cell. imageView setImageWithURL: [NSURL URLWithString: app. icon] placeholderImage: place]; return cell ;}
Thoughts on Asynchronous image download, Sandbox caching, and other operations (interview)

SDWebImage

Introduction:
1: SDWebImage is a famous network image processing framework in iOS.
2: includes functions: Image download, image cache, download progress monitoring, and gif Processing.
3: It is extremely simple to use and has powerful functions, greatly improving the processing efficiency of network images.
4: more than 90% of iOS projects in China have their shadows.

Procedure

Step 1: Download SDWebImage and import the project. Github hosting address https://github.com/rs/SDWebImage

Step 2: import the header file as needed

#import "UIImageView+WebCache.h"

Step 3: Call sd_setImageWithURL: the old method version does not start with "sd.

II. Key points: several common methods to remember: 1. Common Methods
// Set the download link and position image-(void) sd_setImageWithURL :( NSURL *) url placeholderImage :( UIImage *) placeholder;
// Set the download link and position image, and a set of options-(void) sd_setImageWithURL :( NSURL *) url placeholderImage :( UIImage *) placeholder options :( SDWebImageOptions) options;

SDWebImageOptions (options in the above method)
* SDWebImageRetryFailed: After the download fails, the system automatically downloads the image again.
* SDWebImageLowPriority: When UI interaction is ongoing, some internal download operations are automatically suspended.
* SDWebImageRetryFailed | SDWebImageLowPriority: Provides the preceding two functions.

// Retry SDWebImageRetryFailed = 1 <0, // download starts during UI interaction, resulting in delayed download such as UIScrollView slowing down. SDWebImageLowPriority = 1 <1, // only perform 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, // download SDWebImageContinueInBackground = 1 <5, // NSMutableURLRequest. HTTPShouldHandleCookies = YES; SDWebImageHandleCookies = 1 <6, // Invalid SSL certificate is allowed // SDWebImageAllowInvalidSSLCertificates = 1 <7, // download SDWebImageHighPriority = 1 <8, // latency placeholder SDWebImageDelayPlaceholder = 1 <9, // modify the animation image SDWebImageTransformAnimatedImage = 1 <10,

// Set the link for downloading the image and the position image, and then perform the following operations (completed)

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;

// This method is used for the download progress bar

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
2 Memory Processing: When the app receives a memory warning

/**
* When the app receives a memory warning
*/
-(Void) applicationDidReceiveMemoryWarning :( UIApplication *) application
{
SDWebImageManager * mgr = [SDWebImageManager sharedManager];

// 1. Cancel the download operation [mgr cancelAll]; // 2. Clear the memory cache [mgr. imageCache clearMemory];

}

3. Internal implementation process of SDWebImageThird-party framework recommendations

1. Purpose of using a third-party framework
A: development efficiency: rapid development. The encapsulated line of code is written in N lines by yourself.
B: To use this function for the most awesome implementation

2. Too many third-party frameworks, many disadvantages (negligible)
A: management, upgrade, and update
B: The third-party framework has bugs and will be resolved by the author.
C: the author of the third-party framework unfortunately died and the update was stopped (potential bugs are not solved)
D: Feeling: self-satisfied

For example, streaming media needs to play online videos and audios (downloading and playing.
If you need to do it yourself, you need to know the formats of audio and video files. In addition, each video has its own decoding method (C \ C ++), which is really difficult, and there is no result for years of in-depth research.

4. Summary
1> programming on the shoulders of giants
2> It Doesn't Matter. Use a stable third-party framework.

Ps: (the internal implementation process of SDWebImage references the ppt of the 6duxz blog from Chuanzhi)

Ps2, welcome to make friends.

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.