SDWebImageManager.m
3.82 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
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "SDWebImageManager.h"
#import "SDImageCache.h"
#import "SDWebImageDownloader.h"
static SDWebImageManager *instance;
@implementation SDWebImageManager
- (id)init
{
if (self = [super init])
{
delegates = [[NSMutableArray alloc] init];
downloaders = [[NSMutableArray alloc] init];
downloaderForURL = [[NSMutableDictionary alloc] init];
failedURLs = [[NSMutableArray alloc] init];
}
return self;
}
- (void) dealloc
{
[delegates release];
[downloaders release];
[downloaderForURL release];
[failedURLs release];
[super dealloc];
}
+ (id)sharedManager
{
if (instance == nil)
{
instance = [[SDWebImageManager alloc] init];
}
return instance;
}
- (UIImage *)imageWithURL:(NSURL *)url
{
return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
}
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate
{
if ([failedURLs containsObject:url])
{
return;
}
// Share the same downloader for identical URLs so we don't download the same URL several times
SDWebImageDownloader *downloader = [downloaderForURL objectForKey:url];
if (!downloader)
{
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
[downloaderForURL setObject:downloader forKey:url];
}
@synchronized(self)
{
[delegates addObject:delegate];
[downloaders addObject:downloader];
}
}
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
{
@synchronized(self)
{
NSUInteger index = [delegates indexOfObjectIdenticalTo:delegate];
if (index == NSNotFound)
{
return;
}
SDWebImageDownloader *downloader = [[downloaders objectAtIndex:index] retain];
[delegates removeObjectAtIndex:index];
[downloaders removeObjectAtIndex:index];
if (![downloaders containsObject:downloader])
{
NSLog(@"cancel download");
// No more delegate are waiting for this download, cancel it
[downloader cancel];
[downloaderForURL removeObjectForKey:downloader.url];
}
[downloader release];
}
}
- (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image
{
[downloader retain];
@synchronized(self)
{
// Notify all the delegates with this downloader
for (NSInteger index = [downloaders count] - 1; index >= 0; index--)
{
SDWebImageDownloader *aDownloader = [downloaders objectAtIndex:index];
if (aDownloader == downloader)
{
id<SDWebImageManagerDelegate> delegate = [delegates objectAtIndex:index];
if (image && [delegate respondsToSelector:@selector(imageHelper:didFinishWithImage:)])
{
[delegate performSelector:@selector(imageHelper:didFinishWithImage:) withObject:self withObject:image];
}
[downloaders removeObjectAtIndex:index];
[delegates removeObjectAtIndex:index];
}
}
}
if (image)
{
// Store the image in the cache
[[SDImageCache sharedImageCache] storeImage:image forKey:[downloader.url absoluteString]];
}
else
{
// The image can't be downloaded from this URL, mark the URL as failed so we won't try and fail again and again
[failedURLs addObject:downloader.url];
}
// Release the downloader
[downloaderForURL removeObjectForKey:downloader.url];
[downloader release];
}
@end