-
Notifications
You must be signed in to change notification settings - Fork 187
/
ImagesViewController.m
106 lines (85 loc) · 3.15 KB
/
ImagesViewController.m
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
//
// ImagesViewController.m
// AsyncImageDemo
//
// Created by Nick Lockwood on 17/10/2011.
// Copyright (c) 2011 Charcoal Design. All rights reserved.
//
#import "ImagesViewController.h"
#import "ImageViewController.h"
#import "AsyncImageView.h"
@implementation ImagesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//set title
self.navigationItem.title = self.navigationController.tabBarItem.title;
//get image URLs
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Images" ofType:@"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
//remote image URLs
NSMutableArray *URLs = [NSMutableArray array];
for (NSString *path in imagePaths)
{
NSURL *URL = [NSURL URLWithString:path];
if (URL)
{
[URLs addObject:URL];
}
else
{
NSLog(@"'%@' is not a valid URL", path);
}
}
self.imageURLs = URLs;
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//unload view to demonstrate caching
self.view = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_imageURLs count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static const NSInteger IMAGE_VIEW_TAG = 99;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//add AsyncImageView to cell
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = IMAGE_VIEW_TAG;
[cell addSubview:imageView];
//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.indentationWidth = 44.0f;
cell.indentationLevel = 1;
}
//get image view
AsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
//load the image
imageView.imageURL = [_imageURLs objectAtIndex:indexPath.row];
//display image path
cell.textLabel.text = [[[_imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];
return cell;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
ImageViewController *viewController = [[ImageViewController alloc] init];
[viewController view]; // load view
viewController.imageView.imageURL = [_imageURLs objectAtIndex:indexPath.row];
viewController.title = [[[_imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];
[self.navigationController pushViewController:viewController animated:YES];
}
@end