Web Image
=========
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
- An UIImageView category adding web image and cache management to the Cocoa Touch framework
- An asynchronous image downloader
- An asynchronous memory + disk image caching with automatic cache expiration handling
- Animated GIF support
- WebP format support
- A background image decompression
- A guarantee that the same URL won't be downloaded several times
- A guarantee that bogus URLs won't be retried again and again
- A guarantee that main thread will never be blocked
- Performances!
- Use GCD and ARC
- Arm64 support
NOTE: The version 3.0 of SDWebImage isn't fully backward compatible with 2.0 and requires iOS 5.1.1
minimum deployement version. If you need iOS < 5.0 support, please use the last [2.0 version](https://github.com/rs/SDWebImage/tree/2.0-compat).
[How is SDWebImage better than X?](https://github.com/rs/SDWebImage/wiki/How-is-SDWebImage-better-than-X%3F)
Who Use It
----------
Find out [who uses SDWebImage](https://github.com/rs/SDWebImage/wiki/Who-Uses-SDWebImage) and add your app to the list.
How To Use
----------
API documentation is available at [http://hackemist.com/SDWebImage/doc/](http://hackemist.com/SDWebImage/doc/)
### Using UIImageView+WebCache category with UITableView
Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage:
method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be
handled for you, from async downloads to caching management.
```objective-c
#import <SDWebImage/UIImageView+WebCache.h>
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
```
### Using blocks
With blocks, you can be notified about the image download progress and whenever the image retrival
has completed with success or not:
```objective-c
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
```
Note: neither your success nor failure block will be call if your image request is canceled before completion.
### Using SDWebImageManager
The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the
asynchronous downloader with the image cache store. You can use this class directly to benefit
from web image downloading with caching in another context than a UIView (ie: with Cocoa).
Here is a simple example of how to use SDWebImageManager:
```objective-c
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
// do something with image
}
}];
```
### Using Asynchronous Image Downloader Independently
It's also possible to use the async image downloader independently:
```objective-c
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
// do something with image
}
}];
```
### Using Asynchronous Image Caching Independently
It is also possible to use the aync based image cache store independently. SDImageCache
maintains a memory cache and an optional disk cache. Disk cache write operations are performed
asynchronous so it doesn't add unnecessary latency to the UI.
The SDImageCache class provides a singleton instance for convenience but you can create your own
instance if you want to create separated cache namespace.
To lookup the cache, you use the imageForKey: method. If the method returns nil, it means the cache
doesn't currently own the image. You are thus responsible for generating and caching it. The cache
key is an application unique identifier for the image to cache. It is generally the absolute URL of
the image.
```objective-c
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
```
By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache.
You can prevent this from happening by calling the alternative method `imageFromMemoryCacheForKey:`.
To store an image into the cache, you use the storeImage:forKey: method:
```objective-c
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
```
By default, the image will be stored in memory cache as well as on disk cache (asynchronously). If
you want only the memory cache, use the alternative method storeImage:forKey:toDisk: with a negative
third argument.
### Using cache key filter
Sometime, you may not want to use the image URL as cache key because part of the URL is dynamic
(i.e.: for access control purpose). SDWebImageManager provides a way to set a cache key filter that
takes the NSURL as input, and output a cache key NSString.
The following example sets a filter in the application delegate that will remove any query-string from
the URL before to use it as a cache key:
```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SDWebImageManager.sharedManager.cacheKeyFilter:^(NSURL *url)
{
url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
return [url absoluteString];
};
// Your app init code...
return YES;
}
```
Common Problems
---------------
### Using dynamic image size with UITableViewCell
UITableView determins the size of the image by the first image set for a cell. If your remote images
don't have the same size as your placeholder image, you may experience strange anamorphic scaling issue.
The following article gives a way to workaround this issue:
[http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/](http://www.wrichards.com/blog/2011/11/s
没有合适的资源?快使用搜索试试~ 我知道了~
SDWebImage远程图片异步加载
共56个文件
m:18个
h:18个
xcscheme:4个
5星 · 超过95%的资源 需积分: 10 58 下载量 63 浏览量
2014-02-17
18:56:47
上传
评论
收藏 2.83MB ZIP 举报
温馨提示
SDWebImage远程图片异步加载, 使用说明:http://www.cnblogs.com/MartinLi841538513/articles/3553059.html
资源推荐
资源详情
资源评论
收起资源包目录
SDWebImage-master.zip (56个子文件)
SDWebImage-master
.gitignore 214B
README.md 10KB
.gitmodules 102B
Examples
CustomPathImages
4ad9ae8eabfec60b40bf48f0bfc2d120 2.74MB
SDWebImage Demo.xcodeproj
project.xcworkspace
contents.xcworkspacedata 160B
project.pbxproj 18KB
Default-568h@2x.png 18KB
SDWebImage Demo
MasterViewController.m 38KB
SDWebImage Demo-Info.plist 1KB
DetailViewController.h 352B
AppDelegate.h 367B
placeholder.png 3KB
SDWebImage Demo-Prefix.pch 333B
en.lproj
DetailViewController.xib 7KB
MasterViewController.xib 6KB
placeholder@2x.png 6KB
main.m 349B
MasterViewController.h 350B
AppDelegate.m 3KB
DetailViewController.m 2KB
LICENSE 1KB
SDWebImage.xcodeproj
project.pbxproj 77KB
xcshareddata
xcschemes
SDWebImageFramework.xcscheme 2KB
SDWebImage+WebP.xcscheme 2KB
SDWebImage.xcscheme 2KB
SDWebImage+MKAnnotation.xcscheme 2KB
SDWebImage.podspec 2KB
Vendors
libwebp
SDWebImage
SDWebImageManager.h 7KB
UIImage+GIF.h 382B
MKAnnotationView+WebCache.h 4KB
UIButton+WebCache.m 5KB
SDWebImageDecoder.m 3KB
SDWebImageDownloaderOperation.m 14KB
SDWebImageDecoder.h 453B
SDWebImagePrefetcher.m 5KB
UIImage+WebP.m 2KB
NSData+ImageContentType.m 1002B
UIImage+MultiFormat.h 263B
SDWebImageDownloader.h 5KB
SDWebImageDownloaderOperation.h 851B
SDImageCache.m 17KB
UIImageView+WebCache.m 5KB
SDWebImagePrefetcher.h 3KB
UIImage+WebP.h 490B
SDWebImageManager.m 10KB
UIImageView+WebCache.h 6KB
NSData+ImageContentType.h 236B
SDWebImageDownloader.m 10KB
UIButton+WebCache.h 9KB
UIImage+MultiFormat.m 839B
SDWebImageCompat.m 1KB
MKAnnotationView+WebCache.m 2KB
SDWebImageOperation.h 329B
SDWebImageCompat.h 1KB
UIImage+GIF.m 5KB
SDImageCache.h 5KB
共 56 条
- 1
li841538513
- 粉丝: 3
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页