1
+
1
2
Web Image
2
3
=========
3
4
@@ -8,9 +9,15 @@ It provides:
8
9
- An UIImageView category adding web image and cache management to the Cocoa Touch framework
9
10
- An asynchronous image downloader
10
11
- An asynchronous memory + disk image caching with automatic cache expiration handling
12
+ - A background image decompression
11
13
- A guarantee that the same URL won't be downloaded several times
12
14
- A guarantee that bogus URLs won't be retried again and again
15
+ - A guarantee that main thread will never be blocked
13
16
- Performances!
17
+ - Use GCD and ARC
18
+
19
+ NOTE: The version 3.0 of SDWebImage isn't fully backward compatible with 1.0 and requires iOS 5.0
20
+ minimum. If you need iOS < 5.0 support, please use the last 2.0 version.
14
21
15
22
Motivation
16
23
----------
@@ -94,15 +101,14 @@ handled for you, from async downloads to caching management.
94
101
95
102
### Using blocks
96
103
97
- If your project's deployement target is set to iOS 4+, you may want to use the success/failure blocks to be
98
- notified when image have been retrieved from cache.
104
+ With blocks, you can be notified about the image download progress and whenever the image retrival
105
+ has completed with success or not:
106
+
99
107
``` objective-c
100
108
// Here we use the new provided setImageWithURL: method to load the web image
101
109
[cell.imageView setImageWithURL: [ NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg "]
102
110
placeholderImage:[ UIImage imageNamed:@"placeholder.png"]
103
- success:^(UIImage * image, BOOL cached) {... success code here ...}
104
- failure:^(NSError * error) {... failure code here ...}] ;
105
- ];
111
+ completed:^(UIImage * image, NSError * error, BOOL fromCache) {... completion code here ...}] ;
106
112
```
107
113
108
114
Note: neither your success nor failure block will be call if your image request is canceled before completion.
@@ -118,29 +124,43 @@ Here is a simple example of how to use SDWebImageManager:
118
124
```objective-c
119
125
SDWebImageManager *manager = [SDWebImageManager sharedManager];
120
126
[manager downloadWithURL:imageURL
121
- delegate: self
122
127
options:0
123
- success :^(UIImage * image, BOOL cached )
128
+ progress :^(NSUInteger receivedSize, long long expectedSize )
124
129
{
125
- // do something with image
130
+ // progression tracking code
126
131
}
127
- failure: nil ] ;
132
+ completed:^(UIImage *image, NSError *error, BOOL fromCache)
133
+ {
134
+ if (image)
135
+ {
136
+ // do something with image
137
+ }
138
+ }];
128
139
```
129
140
130
141
### Using Asynchronous Image Downloader Independently
131
142
132
- It is possible to use the async image downloader independently. You just have to create an instance
133
- of SDWebImageDownloader using its convenience constructor downloaderWithURL:delegate:.
143
+ It's also possible to use the async image downloader independently:
144
+
134
145
``` objective-c
135
- downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
146
+ [SDWebImageDownloader.sharedDownloader downloadImageWithURL: imageURL
147
+ options:0
148
+ progress:^(NSUInteger receivedSize, long long expectedSize)
149
+ {
150
+ // progression tracking code
151
+ }
152
+ completed:^(UIImage * image, NSError * error, BOOL finished)
153
+ {
154
+ if (image && finished)
155
+ {
156
+ // do something with image
157
+ }
158
+ }] ;
136
159
```
137
160
138
- The download will start immediately and the imageDownloader:didFinishWithImage: method from the
139
- SDWebImageDownloaderDelegate protocol will be called as soon as the download of image is completed.
140
-
141
161
### Using Asynchronous Image Caching Independently
142
162
143
- It is also possible to use the NSOperation based image cache store independently. SDImageCache
163
+ It is also possible to use the aync based image cache store independently. SDImageCache
144
164
maintains a memory cache and an optional disk cache. Disk cache write operations are performed
145
165
asynchronous so it doesn't add unnecessary latency to the UI.
146
166
@@ -153,7 +173,11 @@ key is an application unique identifier for the image to cache. It is generally
153
173
the image.
154
174
155
175
```objective-c
156
- UIImage *myCachedImage = [[SDImageCache sharedImageCache ] imageFromKey: myCacheKey] ;
176
+ SDImageCache *imageCache = [SDImageCache.alloc initWithNamespace:@"myNamespace"];
177
+ [imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
178
+ {
179
+ // image is not nil if image was found
180
+ }];
157
181
```
158
182
159
183
By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache.
@@ -182,11 +206,11 @@ the URL before to use it as a cache key:
182
206
```objective-c
183
207
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
184
208
{
185
- [[ SDWebImageManager sharedManager] setCacheKeyFilter :^(NSURL *url)
209
+ SDWebImageManager. sharedManager.cacheKeyFilter :^(NSURL *url)
186
210
{
187
211
url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
188
212
return [url absoluteString];
189
- }] ;
213
+ };
190
214
191
215
// Your app init code...
192
216
return YES;
@@ -205,12 +229,6 @@ The following article gives a way to workaround this issue:
205
229
206
230
[ http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/ ] ( http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/ )
207
231
208
- Automatic Reference Counting (ARC)
209
- ----------------------------------
210
-
211
- You can use either style in your Cocoa project. SDWebImage Will figure out which you are using at compile
212
- time and do the right thing.
213
-
214
232
215
233
Installation
216
234
------------
0 commit comments