sdwebimage的使用
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 frameworkAn asynchronous image downloaderAn asynchronous memory + disk image caching with automatic cache expiration handlingAnimated GIF supportWebP format supportA background image decompressionA guarantee that the same URL won't be downloaded several timesA guarantee that bogus URLs won't be retried again and againA guarantee that main thread will never be blockedPerformances!Use GCD and ARC
NOTE: The version 3.0 of SDWebImage isn't fully backward compatible with 2.0 and requires iOS 5.0 minimum deployement version. If you need iOS < 5.0 support, please use the last?2.0 version.
How is SDWebImage better than X?
Who Use It
Find out?who use SDWebImage?and add your app to the list.
How To Use
API documentation is available at?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.
#import <SDWebImage/UIImageView+WebCache.h>UITableViewCell tableViewUITableView tableView cellForRowAtIndexPathNSIndexPath indexPathNSString MyIdentifier @"MyIdentifier"UITableViewCell cell tableView dequeueReusableCellWithIdentifierMyIdentifiercell nilcell UITableViewCell allocinitWithStyleUITableViewCellStyleDefault MyIdentifierautorelease// Here we use the new provided setImageWithURL: method to load the web image cellimageView setImageWithURLNSURL URLWithString@"http://www.domain.com/path/to/image.jpg"UIImage imageNamed@"placeholder.png"celltextLabeltext @"My Text"cell// Here we use the new provided setImageWithURL: method to load the web imagecellimageView setImageWithURLNSURL URLWithString@"http://www.domain.com/path/to/image.jpg"UIImage imageNamed@"placeholder.png"UIImage imageNSError errorSDImageCacheType cacheTypecompletion code hereSDWebImageManager manager SDWebImageManager sharedManagermanager downloadWithURLimageURL 0 NSUInteger receivedSizelong long expectedSize// progression tracking code UIImage imageNSError errorSDImageCacheType cacheTypeimage// do something with imageSDWebImageDownloadersharedDownloader downloadImageWithURLimageURL 0 NSUInteger receivedSizelong long expectedSize// progression tracking code UIImage imageNSError errorBOOL finishedimage finished// do something with imageSDImageCache imageCache SDImageCachealloc initWithNamespace@"myNamespace"imageCache queryDiskCacheForKeymyCacheKey doneUIImage image// image is not nil if image was foundSDImageCache sharedImageCachestoreImagemyImage forKeymyCacheKeyBOOLapplication:UIApplication application didFinishLaunchingWithOptions:NSDictionary launchOptionsSDWebImageManagersharedManagercacheKeyFilterNSURL urlurl NSURL allocinitWithSchemeurlscheme hosturlhost pathurlpathautoreleaseurl absoluteString// Your app init code... YESimageView setImageWithURLNSURL URLWithString@"https://graph.facebook.com/olivier.poitrey/picture"UIImage imageNamed@"avatar-placeholder.png"SDWebImageRefreshCachedAdd dependencies
In you application project app’s target settings, find the "Build Phases" section and open the "Link Binary With Libraries" block:Click the "+" button again and select the "ImageIO.framework", this is needed by the progressive download feature:
Add Linker Flag
Open the "Build Settings" tab, in the "Linking" section, locate the "Other Linker Flags" setting and add the "-ObjC" flag:
Import headers in your source files
In the source files where you need to use the library, import the header file:
#import <SDWebImage/UIImageView+WebCache.h>Build Project
At this point your workspace should build without error. If you are having problem, post to the Issue and the community can help you solve it.
Future Enhancements
LRU memory cache cleanup instead of reset on memory warning
