DMWebImageView.m
1.58 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
/*
* This file is part of the DMWebImage package.
* (c) Dailymotion - 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 "DMWebImageView.h"
#import "DMImageCache.h"
#import "DMWebImageDownloader.h"
@implementation DMWebImageView
- (void)dealloc
{
[placeHolderImage release];
[downloader release];
[super dealloc];
}
#pragma mark RemoteImageView
- (void)setImageWithURL:(NSURL *)url
{
if (downloader != nil)
{
// Remove in progress downloader from queue
[downloader cancel];
[downloader release];
downloader = nil;
}
// Save the placeholder image in order to re-apply it when view is reused
if (placeHolderImage == nil)
{
placeHolderImage = [self.image retain];
}
else
{
self.image = placeHolderImage;
}
UIImage *cachedImage = [[DMImageCache sharedImageCache] imageFromKey:[url absoluteString]];
if (cachedImage)
{
self.image = cachedImage;
}
else
{
downloader = [[DMWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain];
}
}
- (void)downloadFinishedWithImage:(UIImage *)anImage
{
// Apply image to the underlaying UIImageView
self.image = anImage;
// Store the image in the cache
[[DMImageCache sharedImageCache] storeImage:anImage forKey:[downloader.url absoluteString]];
// Free the downloader
[downloader release];
downloader = nil;
}
@end