SDWebImage-圖片自動(dòng)緩存、異步加載實(shí)用庫

SDWebImage是一個(gè)可以自動(dòng)管理圖片加載的類庫。

因?yàn)榇蠹叶贾溃瑘D片加載非常耗流量,所以在移動(dòng)平臺(tái)上對(duì)于圖片的處理就要異常小心了。因此就必須用到本地緩存了。

而我之前寫的一個(gè)小型App,因?yàn)槊繌垐D片的名字都是GUID生成的,不會(huì)被修改了。所以會(huì)每次都檢查一下本地是否有這個(gè)文件名的文件存在了。這樣最大限度的減小了網(wǎng)絡(luò)流量,不需要每次都加載一次。

不過呢,SDWebImage的功能不僅僅僅限于此,功能更為強(qiáng)大。最基本的有一個(gè)UIImageView的category,用法很簡單

[imageView setImageWithURL:[NSURLURLWithString:@"http://www.ioslib.com/ioslib.png"]];

另外呢,還有一個(gè)SDWebImageManager,使用它可以進(jìn)行一些異步加載的工作,關(guān)于這部分內(nèi)容,可以參見官方的文檔:

https://github.com/rs/SDWebImage#readme

SDWebImage是托管在Github上的:http://github.com/rs/SDWebImage

SDWebImage——簡化網(wǎng)絡(luò)圖片處理

用SDWebImage調(diào)用網(wǎng)站上的圖片,跟本地調(diào)用內(nèi)置在應(yīng)用包里的圖片一樣簡單。操作也很簡單,舉例說明

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 behandled for you, from async downloads to caching management.

#import "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 SDWebImageManager

The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties theasynchronous downloader with the image cache store. You can use this classe directly to benefitsfrom web image downloading with caching in another context than a UIView (ie: with Cocos).

Here is a simple example of how to use SDWebImageManager:

SDWebImageManager *manager = [SDWebImageManager sharedManager];

UIImage *cachedImage = [manager imageWithURL:url];

if (cachedImage)

{

// Use the cached image immediatly

}

else

{

// Start an async download

[manager downloadWithURL:url delegate:self];

}

Your class will have to implement the SDWebImageManagerDelegate protocol, and to implement thewebImageManager:didFinishWithImage: method from this protocol:

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image

{

// Do something with the downloaded image

}

Using Asynchronous Image Downloader Independently

It is possible to use the async image downloader independently. You just have to create an instanceof SDWebImageDownloader using its convenience constructor downloaderWithURL:delegate:.

downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];

The download will start immediately and the imageDownloader:didFinishWithImage: method from theSDWebImageDownloaderDelegate protocol will be called as soon as the download of image is completed.

Using Asynchronous Image Caching Independently

It is also possible to use the NSOperation based image cache store independently. SDImageCachemaintains a memory cache and an optional disk cache. Disk cache write operations are performedasynchronous so it doesn't add unnecessary latency to the UI.

The SDImageCache class provides a singleton instance for convenience but you can create your owninstance if you want to create separated cache namespaces.

To lookup the cache, you use the imageForKey: method. If the method returns nil, it means the cachedoesn't currently own the image. You are thus responsible of generating and caching it. The cachekey is an application unique identifier for the image to cache. It is generally the absolute URL ofthe image.

UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];

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 imageFromKey:fromDisk: with anegative second argument.

To store an image into the cache, you use the storeImage:forKey: method:

[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

By default, the image will be stored in memory cache as well as on disk cache (asynchronously). Ifyou want only the memory cache, use the alternative method storeImage:forKey:toDisk: with a negativethird argument.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容