forked from iimgal/StudyiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueViewController.m
More file actions
181 lines (154 loc) · 5.77 KB
/
QueueViewController.m
File metadata and controls
181 lines (154 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// QueueViewController.m
// StudyiOS
//
// Created by on 11-10-16.
// Copyright (c) 2011年 ZhangYiCheng. All rights reserved.
//
#import "QueueViewController.h"
// Private stuff
@interface QueueViewController ()
- (void)imageFetchComplete:(ASIHTTPRequest *)request;
- (void)imageFetchFailed:(ASIHTTPRequest *)request;
@end
@implementation QueueViewController
@synthesize networkQueue;
@synthesize imageView1;
@synthesize imageView2;
@synthesize imageView3;
@synthesize progressIndicator;
@synthesize accurateProgress;
@synthesize imageProgressIndicator1;
@synthesize imageProgressIndicator2;
@synthesize imageProgressIndicator3;
@synthesize imageLabel1;
@synthesize imageLabel2;
@synthesize imageLabel3;
@synthesize failed;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
self.networkQueue = nil;
self.imageView1 = nil;
self.imageView2 = nil;
self.imageView3 = nil;
self.progressIndicator = nil;
self.accurateProgress = nil;
self.imageProgressIndicator1 = nil;
self.imageProgressIndicator2 = nil;
self.imageProgressIndicator3 = nil;
self.imageLabel1 = nil;
self.imageLabel2 = nil;
self.imageLabel3 = nil;
[super viewDidUnload];
}
- (void)dealloc
{
// 退出时清空队列
[networkQueue reset];
}
- (void)viewDidLoad
{
// 增加Code按钮,可跳转至教学页面
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Code" style:UIBarButtonItemStyleBordered target:self action:@selector(code)];
//self.navigationItem.rightBarButtonItem = item;
self.tabBarController.navigationItem.rightBarButtonItem = item;
[super viewDidLoad];
}
- (IBAction)fetchThreeImages:(id)sender
{
// UI清空
[imageView1 setImage:nil];
[imageView2 setImage:nil];
[imageView3 setImage:nil];
self.progressIndicator.progress = 0.0;
self.imageProgressIndicator1.progress = 0.0;
self.imageProgressIndicator2.progress = 0.0;
self.imageProgressIndicator3.progress = 0.0;
// 初始化队列
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
// 设置最大并发数 默认为-1 无限制
[networkQueue setMaxConcurrentOperationCount:10];
}
failed = NO;
// 重制队列
[networkQueue reset];
// 设置队列的进度条
[networkQueue setDownloadProgressDelegate:progressIndicator];
// 设置完成方法
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
// 设置错误方法
[networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)];
// 显示精确进度
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];
// 初始化请求
ASIHTTPRequest *request;
// 设置请求URL
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
// 设置下载的目标路径
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
// 设置进度条
[request setDownloadProgressDelegate:imageProgressIndicator1];
// 自定义用户信息
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
// 将请求加入队列
[networkQueue addOperation:request];
// 同上
request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator2];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]];
[networkQueue addOperation:request];
// 同上
request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator3];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request3" forKey:@"name"]];
[networkQueue addOperation:request];
// 启动队列
[networkQueue go];
}
// 请求成功后更新UI
- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
// 从下载路径里读取文件
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
NSString *name = [[request userInfo] valueForKey:@"name"];
if (img) {
if ([name isEqualToString:@"request1"]) {
[imageView1 setImage:img];
}else if([name isEqualToString:@"request2"]){
[imageView2 setImage:img];
}else if([name isEqualToString:@"request3"]){
[imageView3 setImage:img];
}
}
}
// 请求失败后调用
- (void)imageFetchFailed:(ASIHTTPRequest *)request
{
if (!failed) {
if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
failed = YES;
}
}
// 跳转至教学页面
- (void)code
{
CodeViewController *controller = [[CodeViewController alloc] init];
NSString *name = [NSString stringWithUTF8String:object_getClassName(self)];
controller.className = name;
[self.navigationController pushViewController:controller animated:YES];
}
@end