UIButton+WebCache.m
6.03 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* 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 "UIButton+WebCache.h"
#import "SDWebImageManager.h"
@implementation UIButton (WebCache)
- (void)setImageWithURL:(NSURL *)url
{
[self setImageWithURL:url placeholderImage:nil];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
[self setImageWithURL:url placeholderImage:placeholder options:0];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// Remove in progress downloader from queue
[manager cancelForDelegate:self];
[self setImage:placeholder forState:UIControlStateNormal];
[self setImage:placeholder forState:UIControlStateSelected];
[self setImage:placeholder forState:UIControlStateHighlighted];
if (url)
{
[manager downloadWithURL:url delegate:self options:options];
}
}
#if NS_BLOCKS_AVAILABLE
- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
[self setImageWithURL:url placeholderImage:nil success:success failure:failure];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
[self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// Remove in progress downloader from queue
[manager cancelForDelegate:self];
[self setImage:placeholder forState:UIControlStateNormal];
[self setImage:placeholder forState:UIControlStateSelected];
[self setImage:placeholder forState:UIControlStateHighlighted];
if (url)
{
[manager downloadWithURL:url delegate:self options:options success:success failure:failure];
}
}
#endif
- (void)setBackgroundImageWithURL:(NSURL *)url
{
[self setBackgroundImageWithURL:url placeholderImage:nil];
}
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
[self setBackgroundImageWithURL:url placeholderImage:placeholder options:0];
}
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// Remove in progress downloader from queue
[manager cancelForDelegate:self];
[self setBackgroundImage:placeholder forState:UIControlStateNormal];
[self setBackgroundImage:placeholder forState:UIControlStateSelected];
[self setBackgroundImage:placeholder forState:UIControlStateHighlighted];
if (url)
{
NSDictionary *info = [NSDictionary dictionaryWithObject:@"background" forKey:@"type"];
[manager downloadWithURL:url delegate:self options:options userInfo:info];
}
}
#if NS_BLOCKS_AVAILABLE
- (void)setBackgroundImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
[self setBackgroundImageWithURL:url placeholderImage:nil success:success failure:failure];
}
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
[self setBackgroundImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
}
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure;
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
// Remove in progress downloader from queue
[manager cancelForDelegate:self];
[self setBackgroundImage:placeholder forState:UIControlStateNormal];
[self setBackgroundImage:placeholder forState:UIControlStateSelected];
[self setBackgroundImage:placeholder forState:UIControlStateHighlighted];
if (url)
{
NSDictionary *info = [NSDictionary dictionaryWithObject:@"background" forKey:@"type"];
[manager downloadWithURL:url delegate:self options:options userInfo:info success:success failure:failure];
}
}
#endif
- (void)cancelCurrentImageLoad
{
@synchronized(self)
{
[[SDWebImageManager sharedManager] cancelForDelegate:self];
}
}
- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url userInfo:(NSDictionary *)info
{
if ([[info valueForKey:@"type"] isEqualToString:@"background"])
{
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateSelected];
[self setBackgroundImage:image forState:UIControlStateHighlighted];
}
else
{
[self setImage:image forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateSelected];
[self setImage:image forState:UIControlStateHighlighted];
}
}
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image forURL:(NSURL *)url userInfo:(NSDictionary *)info
{
if ([[info valueForKey:@"type"] isEqualToString:@"background"])
{
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateSelected];
[self setBackgroundImage:image forState:UIControlStateHighlighted];
}
else
{
[self setImage:image forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateSelected];
[self setImage:image forState:UIControlStateHighlighted];
}
}
@end