forked from jakemarsh/JMImageCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImageView+JMImageCache.m
109 lines (83 loc) · 3.28 KB
/
UIImageView+JMImageCache.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
107
108
109
//
// UIImageView+JMImageCache.m
// JMImageCacheDemo
//
// Created by Jake Marsh on 7/23/12.
// Copyright (c) 2012 Jake Marsh. All rights reserved.
//
#import "UIImageView+JMImageCache.h"
#import "JMImageCache.h"
#import <objc/runtime.h>
static char kJMImageURLObjectKey;
@interface UIImageView (_JMImageCache)
@property (readwrite, nonatomic, retain, setter = jm_setImageURL:) NSURL *jm_imageURL;
@end
@implementation UIImageView (_JMImageCache)
@dynamic jm_imageURL;
@end
@implementation UIImageView (JMImageCache)
#pragma mark - Private Setters
- (NSURL *) jm_imageURL {
return (NSURL *)objc_getAssociatedObject(self, &kJMImageURLObjectKey);
}
- (void) jm_setImageURL:(NSURL *)imageURL {
objc_setAssociatedObject(self, &kJMImageURLObjectKey, imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - Public Methods
- (void) setImageWithURL:(NSURL *)url {
[self setImageWithURL:url placeholder:nil];
}
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage {
[self setImageWithURL:url key:nil placeholder:placeholderImage];
}
- (void) setImageWithURL:(NSURL *)url placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *))completionBlock {
[self setImageWithURL:url key:nil placeholder:placeholderImage completionBlock:completionBlock];
}
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage {
[self setImageWithURL:url key:key placeholder:placeholderImage completionBlock:nil];
}
- (void) setImageWithURL:(NSURL *)url key:(NSString*)key placeholder:(UIImage *)placeholderImage completionBlock:(void (^)(UIImage *image))completionBlock {
self.jm_imageURL = url;
self.image = placeholderImage;
[self setNeedsDisplay];
[self setNeedsLayout];
__weak UIImageView *safeSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *i;
if (key) {
i = [[JMImageCache sharedCache] cachedImageForKey:key];
} else {
i = [[JMImageCache sharedCache] cachedImageForURL:url];
}
if(i) {
dispatch_async(dispatch_get_main_queue(), ^{
safeSelf.jm_imageURL = nil;
safeSelf.image = i;
[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
safeSelf.image = placeholderImage;
[safeSelf setNeedsDisplay];
[safeSelf setNeedsLayout];
});
[[JMImageCache sharedCache] imageForURL:url key:key completionBlock:^(UIImage *image) {
if ([url isEqual:safeSelf.jm_imageURL]) {
dispatch_async(dispatch_get_main_queue(), ^{
if(image) {
safeSelf.image = image;
} else {
safeSelf.image = placeholderImage;
}
safeSelf.jm_imageURL = nil;
[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
if (completionBlock) completionBlock(image);
});
}
}];
}
});
}
@end