IOS Multi-threaded implementation of multiple picture download (i) _ios

Source: Internet
Author: User

Before entering the body of the text to show you the effect of the picture, if we feel very satisfied please continue to read the full text.

We can see that this interface is very simple, in fact, is the layout of the UITableView, but the difficulty is how to download these pictures from the Internet, how to store after downloading!

We are step-by-step parsing, first from a single thread (the main thread) for multiple pictures Download the text of our layout and the address of the picture from the plist file to read

According to the structure, we customize a data model file

DDZApp.h

#import <Foundation/Foundation.h>
@interface ddzapp:nsobject
//Icon
@property (Nonatomic,strong) NSString *icon;
The name
@property (nonatomic,strong) nsstring *name;
Download Volume
@property (nonatomic,strong) nsstring *download;
+ (Instancetype) appwithdict: (Nsdictionary *) dict;

ddzapp.m

#import "DDZApp.h"
@implementation ddzapp
+ (instancetype) appwithdict: (Nsdictionary *) dict {
Ddzapp * App = [[Self alloc] init];
[App Setvaluesforkeyswithdictionary:dict];
return app;
}

The following are all the code in the View controller

Viewcontroller.m

1.

@interface Viewcontroller ()
//All Data
@property (nonatomic,strong) Nsarray *apps;
Memory Cache picture
@property (nonatomic,strong) nsmutabledictionary *imgcache;

The first property is used to store the contents of the read Plist file and is set to save the property so that you do not have to read it repeatedly

The second property is used to save the pictures downloaded from the web, but also to not read them repeatedly

2.

@implementation Viewcontroller
//Read Data
-(Nsarray *) Apps {
if (!_apps) {
//Read data from plist file
Nsarray * Dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Apps.plist" OfType:nil]];
Nsmutablearray *apparray = [Nsmutablearray array];
For (Nsdictionary *dict in Dictarray) {
[Apparray Addobject:[ddzapp appwithdict:dict]];
}
_apps = Apparray;
}
return _apps;
}
Cache picture
-(nsmutabledictionary *) Imgcache {
if (!_imgcache) {
//Initialize
_imgcache = [ Nsmutabledictionary dictionary];
}
return _imgcache;
}

Both of these methods are intended to initialize the two attributes that were just

3.

#pragma mark-Data source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return s
Elf.apps.count; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {static
NSString *id = @ "App";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];
Ddzapp *app = Self.apps[indexpath.row];
Cell.textLabel.text = App.name;
Cell.detailTextLabel.text = App.download;
First remove the picture from memory uiimage *image = Self.imgcache[app.icon]; if (image) {cell.imageView.image = image;} else {//memory no picture//write picture file data to sandbox nsstring *cachespath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory,
Nsuserdomainmask, YES) firstobject];
Get filename NSString *filename = [App.icon lastpathcomponent];
Calculates the full path of the file NSString *file = [Cachespath stringbyappendingpathcomponent:filename];
Loading sandbox file data nsdata *data = [NSData datawithcontentsoffile:file]; Determine if there are pictures in the sandbox if (data) {//directly loading the picture in the sandbox cell.imageView.image = [UIImage imagewithdata:dATA];
Save in Dictionary (memory) Self.imgcache[app.icon] = Cell.imageView.image; }else {///download picture data = [NSData Datawithcontentsofurl:[nsurl URLWithString:app.icon]]; cell.imageView.image = [UIImage imag
Ewithdata:data];
Save in memory Self.imgcache[app.icon] = cell.imageView.image;
Write the picture data to the sandbox [data writetofile:file atomically:yes];
} return cell; }

These two methods are the methods that UITableView must implement

The first is to return the amount of data, nothing to say

The second one is binding data

The detailed process looks at the following figure

The above content for the iOS multi-threaded implementation of multiple picture download (i) related to the introduction, I hope to help you, the next article to continue to introduce iOS multithreading to achieve multiple pictures download (ii), interested friends Please continue to pay attention.

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.