SDWebImage-圖片自動緩衝、非同步載入實用庫(轉貼)

來源:互聯網
上載者:User
文章目錄
  • Using SDWebImageManager
  • Using Asynchronous Image Downloader Independently
  • Using Asynchronous Image Caching Independently

SDWebImage是一個可以自動管理圖片載入的類庫。


因為大家都知道,圖片載入非常耗流量,所以在移動平台上對於圖片的處理就要異常小心了。因此就必須用到本機快取了。


而我之前寫的一個小型App,因為每張圖片的名字都是GUID產生的,不會被修改了。所以會每次都檢查一下本地是否有這個檔案名稱的檔案存在了。這樣最大限度的減小了網路流量,不需要每次都載入一次。


不過呢,SDWebImage的功能不僅僅僅限於此,功能更為強大。最基本的有一個UIImageView的category,用法很簡單

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

另外呢,還有一個SDWebImageManager,使用它可以進行一些非同步載入的工作,關於這部分內容,可以參見官方的文檔:

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

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

SDWebImage——簡化網狀圖片處理

用SDWebImage調用網站上的圖片,跟本地調用內建在應用程式套件裡的圖片一樣簡單。操作也很簡單,舉例說明

 

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.

轉自 http://www.isdada.com/sdwebimage.html 


偶然所得,不敢獨享,與大家分享之~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.