Skip to content

Commit 2227266

Browse files
author
Olivier Poitrey
committed
First beta of SDWebImage 3.0
This version is a rewrite of the lib using iOS 5.0 as minimum target. The lib as thus been refactored to use blocks/GCD from the ground up. As a consequence, all delegate code style has been removed. The result is a lot less lines of code and more flexibility.
1 parent 8963f99 commit 2227266

25 files changed

+993
-1826
lines changed

README.md

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
Web Image
23
=========
34

@@ -8,9 +9,15 @@ It provides:
89
- An UIImageView category adding web image and cache management to the Cocoa Touch framework
910
- An asynchronous image downloader
1011
- An asynchronous memory + disk image caching with automatic cache expiration handling
12+
- A background image decompression
1113
- A guarantee that the same URL won't be downloaded several times
1214
- A guarantee that bogus URLs won't be retried again and again
15+
- A guarantee that main thread will never be blocked
1316
- 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.
1421

1522
Motivation
1623
----------
@@ -94,15 +101,14 @@ handled for you, from async downloads to caching management.
94101

95102
### Using blocks
96103

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+
99107
```objective-c
100108
// Here we use the new provided setImageWithURL: method to load the web image
101109
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
102110
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 ...}];
106112
```
107113
108114
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:
118124
```objective-c
119125
SDWebImageManager *manager = [SDWebImageManager sharedManager];
120126
[manager downloadWithURL:imageURL
121-
delegate:self
122127
options:0
123-
success:^(UIImage *image, BOOL cached)
128+
progress:^(NSUInteger receivedSize, long long expectedSize)
124129
{
125-
// do something with image
130+
// progression tracking code
126131
}
127-
failure:nil];
132+
completed:^(UIImage *image, NSError *error, BOOL fromCache)
133+
{
134+
if (image)
135+
{
136+
// do something with image
137+
}
138+
}];
128139
```
129140

130141
### Using Asynchronous Image Downloader Independently
131142

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+
134145
```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+
}];
136159
```
137160
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-
141161
### Using Asynchronous Image Caching Independently
142162
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
144164
maintains a memory cache and an optional disk cache. Disk cache write operations are performed
145165
asynchronous so it doesn't add unnecessary latency to the UI.
146166
@@ -153,7 +173,11 @@ key is an application unique identifier for the image to cache. It is generally
153173
the image.
154174
155175
```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+
}];
157181
```
158182

159183
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:
182206
```objective-c
183207
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
184208
{
185-
[[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url)
209+
SDWebImageManager.sharedManager.cacheKeyFilter:^(NSURL *url)
186210
{
187211
url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
188212
return [url absoluteString];
189-
}];
213+
};
190214
191215
// Your app init code...
192216
return YES;
@@ -205,12 +229,6 @@ The following article gives a way to workaround this issue:
205229

206230
[http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/](http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/)
207231

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-
214232

215233
Installation
216234
------------

0 commit comments

Comments
 (0)