root / asi-http-request-with-pithos / iPhone Sample / QueueViewController.m @ be116d22
History | View | Annotate | Download (10 kB)
1 |
// |
---|---|
2 |
// QueueViewController.m |
3 |
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details |
4 |
// |
5 |
// Created by Ben Copsey on 07/11/2008. |
6 |
// Copyright 2008 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
|
9 |
#import "QueueViewController.h" |
10 |
#import "ASIHTTPRequest.h" |
11 |
#import "ASINetworkQueue.h" |
12 |
#import "InfoCell.h" |
13 |
#import "ToggleCell.h" |
14 |
|
15 |
// Private stuff |
16 |
@interface QueueViewController () |
17 |
- (void)imageFetchComplete:(ASIHTTPRequest *)request; |
18 |
- (void)imageFetchFailed:(ASIHTTPRequest *)request; |
19 |
@end |
20 |
|
21 |
@implementation QueueViewController |
22 |
|
23 |
- (IBAction)fetchThreeImages:(id)sender |
24 |
{ |
25 |
[imageView1 setImage:nil]; |
26 |
[imageView2 setImage:nil]; |
27 |
[imageView3 setImage:nil]; |
28 |
|
29 |
if (!networkQueue) { |
30 |
networkQueue = [[ASINetworkQueue alloc] init]; |
31 |
} |
32 |
failed = NO; |
33 |
[networkQueue reset]; |
34 |
[networkQueue setDownloadProgressDelegate:progressIndicator]; |
35 |
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)]; |
36 |
[networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)]; |
37 |
[networkQueue setShowAccurateProgress:[accurateProgress isOn]]; |
38 |
[networkQueue setDelegate:self]; |
39 |
|
40 |
ASIHTTPRequest *request; |
41 |
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]]; |
42 |
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]]; |
43 |
[request setDownloadProgressDelegate:imageProgressIndicator1]; |
44 |
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; |
45 |
[networkQueue addOperation:request]; |
46 |
|
47 |
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease]; |
48 |
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]]; |
49 |
[request setDownloadProgressDelegate:imageProgressIndicator2]; |
50 |
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]]; |
51 |
[networkQueue addOperation:request]; |
52 |
|
53 |
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease]; |
54 |
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]]; |
55 |
[request setDownloadProgressDelegate:imageProgressIndicator3]; |
56 |
[request setUserInfo:[NSDictionary dictionaryWithObject:@"request3" forKey:@"name"]]; |
57 |
[networkQueue addOperation:request]; |
58 |
|
59 |
[networkQueue go]; |
60 |
} |
61 |
|
62 |
|
63 |
- (void)imageFetchComplete:(ASIHTTPRequest *)request |
64 |
{ |
65 |
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]]; |
66 |
if (img) { |
67 |
if ([imageView1 image]) { |
68 |
if ([imageView2 image]) { |
69 |
[imageView3 setImage:img]; |
70 |
} else { |
71 |
[imageView2 setImage:img]; |
72 |
} |
73 |
} else { |
74 |
[imageView1 setImage:img]; |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
- (void)imageFetchFailed:(ASIHTTPRequest *)request |
80 |
{ |
81 |
if (!failed) { |
82 |
if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) { |
83 |
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; |
84 |
[alertView show]; |
85 |
} |
86 |
failed = YES; |
87 |
} |
88 |
} |
89 |
|
90 |
- (void)dealloc { |
91 |
[progressIndicator release]; |
92 |
[networkQueue reset]; |
93 |
[networkQueue release]; |
94 |
[super dealloc]; |
95 |
} |
96 |
|
97 |
|
98 |
/* |
99 |
Most of the code below here relates to the table view, and isn't that interesting |
100 |
*/ |
101 |
|
102 |
- (void)viewDidLoad |
103 |
{ |
104 |
[super viewDidLoad]; |
105 |
[[[self navigationBar] topItem] setTitle:@"Using a Queue"]; |
106 |
} |
107 |
|
108 |
static NSString *intro = @"Demonstrates a fetching 3 items at once, using an ASINetworkQueue to track progress.\r\nEach request has its own downloadProgressDelegate, and the queue has an additional downloadProgressDelegate to track overall progress."; |
109 |
|
110 |
- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section |
111 |
{ |
112 |
if (section == 1) { |
113 |
int tablePadding = 40; |
114 |
int tableWidth = [tableView frame].size.width; |
115 |
if (tableWidth > 480) { // iPad |
116 |
tablePadding = 110; |
117 |
} |
118 |
|
119 |
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease]; |
120 |
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
121 |
[goButton setTitle:@"Go!" forState:UIControlStateNormal]; |
122 |
[goButton sizeToFit]; |
123 |
[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)]; |
124 |
|
125 |
[goButton addTarget:self action:@selector(fetchThreeImages:) forControlEvents:UIControlEventTouchUpInside]; |
126 |
[view addSubview:goButton]; |
127 |
|
128 |
if (!progressIndicator) { |
129 |
progressIndicator = [[UIProgressView alloc] initWithFrame:CGRectMake((tablePadding/2)-10,20,200,10)]; |
130 |
} else { |
131 |
[progressIndicator setFrame:CGRectMake((tablePadding/2)-10,20,200,10)]; |
132 |
} |
133 |
[view addSubview:progressIndicator]; |
134 |
|
135 |
return view; |
136 |
} |
137 |
return nil; |
138 |
} |
139 |
|
140 |
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
141 |
{ |
142 |
int tablePadding = 40; |
143 |
int tableWidth = [tableView frame].size.width; |
144 |
if (tableWidth > 480) { // iPad |
145 |
tablePadding = 110; |
146 |
} |
147 |
|
148 |
UITableViewCell *cell; |
149 |
if ([indexPath section] == 0) { |
150 |
cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"]; |
151 |
if (!cell) { |
152 |
cell = [InfoCell cell]; |
153 |
} |
154 |
[[cell textLabel] setText:intro]; |
155 |
[cell layoutSubviews]; |
156 |
|
157 |
} else if ([indexPath section] == 1) { |
158 |
cell = [tableView dequeueReusableCellWithIdentifier:@"ToggleCell"]; |
159 |
if (!cell) { |
160 |
cell = [ToggleCell cell]; |
161 |
} |
162 |
[[cell textLabel] setText:@"Show Accurate Progress"]; |
163 |
accurateProgress = [(ToggleCell *)cell toggle]; |
164 |
|
165 |
} else { |
166 |
|
167 |
cell = [tableView dequeueReusableCellWithIdentifier:@"ImagesCell"]; |
168 |
|
169 |
|
170 |
if (!cell) { |
171 |
|
172 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImagesCell"] autorelease]; |
173 |
|
174 |
imageView1 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease]; |
175 |
[imageView1 setBackgroundColor:[UIColor grayColor]]; |
176 |
[cell addSubview:imageView1]; |
177 |
|
178 |
imageProgressIndicator1 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease]; |
179 |
[cell addSubview:imageProgressIndicator1]; |
180 |
|
181 |
imageLabel1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; |
182 |
if (tableWidth > 480) { |
183 |
[imageLabel1 setText:@"This image is 15KB in size"]; |
184 |
} else { |
185 |
[imageLabel1 setText:@"Img size: 15KB"]; |
186 |
} |
187 |
[imageLabel1 setTextAlignment:UITextAlignmentCenter]; |
188 |
[imageLabel1 setFont:[UIFont systemFontOfSize:11]]; |
189 |
[imageLabel1 setBackgroundColor:[UIColor clearColor]]; |
190 |
[cell addSubview:imageLabel1]; |
191 |
|
192 |
imageView2 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease]; |
193 |
[imageView2 setBackgroundColor:[UIColor grayColor]]; |
194 |
[cell addSubview:imageView2]; |
195 |
|
196 |
imageProgressIndicator2 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease]; |
197 |
[cell addSubview:imageProgressIndicator2]; |
198 |
|
199 |
imageLabel2 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; |
200 |
if (tableWidth > 480) { |
201 |
[imageLabel2 setText:@"This image is 176KB in size"]; |
202 |
} else { |
203 |
[imageLabel2 setText:@"Img size: 176KB"]; |
204 |
} |
205 |
[imageLabel2 setTextAlignment:UITextAlignmentCenter]; |
206 |
[imageLabel2 setFont:[UIFont systemFontOfSize:11]]; |
207 |
[imageLabel2 setBackgroundColor:[UIColor clearColor]]; |
208 |
[cell addSubview:imageLabel2]; |
209 |
|
210 |
imageView3 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease]; |
211 |
[imageView3 setBackgroundColor:[UIColor grayColor]]; |
212 |
[cell addSubview:imageView3]; |
213 |
|
214 |
imageProgressIndicator3 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease]; |
215 |
[cell addSubview:imageProgressIndicator3]; |
216 |
|
217 |
imageLabel3 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; |
218 |
if (tableWidth > 480) { |
219 |
[imageLabel3 setText:@"This image is 1.4MB in size"]; |
220 |
} else { |
221 |
[imageLabel3 setText:@"Img size: 1.4MB"]; |
222 |
} |
223 |
[imageLabel3 setTextAlignment:UITextAlignmentCenter]; |
224 |
[imageLabel3 setFont:[UIFont systemFontOfSize:11]]; |
225 |
[imageLabel3 setBackgroundColor:[UIColor clearColor]]; |
226 |
[cell addSubview:imageLabel3]; |
227 |
|
228 |
} |
229 |
NSUInteger imageWidth = (tableWidth-tablePadding-20)/3; |
230 |
NSUInteger imageHeight = imageWidth*0.66f; |
231 |
|
232 |
|
233 |
[imageView1 setFrame:CGRectMake(tablePadding/2,10,imageWidth,imageHeight)]; |
234 |
[imageProgressIndicator1 setFrame:CGRectMake(tablePadding/2,15+imageHeight,imageWidth,20)]; |
235 |
[imageLabel1 setFrame:CGRectMake(tablePadding/2,25+imageHeight,imageWidth,20)]; |
236 |
|
237 |
[imageView2 setFrame:CGRectMake((tablePadding/2)+imageWidth+10,10,imageWidth,imageHeight)]; |
238 |
[imageProgressIndicator2 setFrame:CGRectMake((tablePadding/2)+imageWidth+10,15+imageHeight,imageWidth,20)]; |
239 |
[imageLabel2 setFrame:CGRectMake(tablePadding/2+imageWidth+10,25+imageHeight,imageWidth,20)]; |
240 |
|
241 |
[imageView3 setFrame:CGRectMake((tablePadding/2)+(imageWidth*2)+20,10,imageWidth,imageHeight)]; |
242 |
[imageProgressIndicator3 setFrame:CGRectMake((tablePadding/2)+(imageWidth*2)+20,15+imageHeight,imageWidth,20)]; |
243 |
[imageLabel3 setFrame:CGRectMake(tablePadding/2+(imageWidth*2)+20,25+imageHeight,imageWidth,20)]; |
244 |
} |
245 |
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
246 |
return cell; |
247 |
} |
248 |
|
249 |
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section |
250 |
{ |
251 |
return 1; |
252 |
} |
253 |
|
254 |
- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section |
255 |
{ |
256 |
if (section == 1) { |
257 |
return 50; |
258 |
} |
259 |
return 34; |
260 |
} |
261 |
|
262 |
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath |
263 |
{ |
264 |
if ([indexPath section] == 0) { |
265 |
return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20; |
266 |
} else if ([indexPath section] == 2) { |
267 |
int tablePadding = 40; |
268 |
int tableWidth = [tableView frame].size.width; |
269 |
if (tableWidth > 480) { // iPad |
270 |
tablePadding = 110; |
271 |
} |
272 |
NSUInteger imageWidth = (tableWidth-tablePadding-20)/3; |
273 |
NSUInteger imageHeight = imageWidth*0.66f; |
274 |
return imageHeight+50; |
275 |
} else { |
276 |
return 42; |
277 |
} |
278 |
} |
279 |
|
280 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
281 |
{ |
282 |
return 3; |
283 |
} |
284 |
|
285 |
@end |