SDWebImage ReadMe.md文檔簡單說明

jopen 8年前發布 | 12K 次閱讀 iOS開發 移動開發 SDWebImage

SDWebImage ReadMe.md 文檔

附 :SDWebImage框架github下載地址: https://github.com/rs/SDWebImage

注1 :該文章簡單翻譯了SDWebImage最新版本(3.7.3)的readMe.md,時間緊促,如有不當請指正修改,十分感激。

注2 :對該框架的學習將持續進行,在個人的 github地址 可以得到最新進展。

Web Image

版本 3.7.3 |平臺 iOS |開源協議 MIT |

該庫為UIImageView提供了一個分類來處理遠程圖片資源的加載。

它:

1)為Cocoa Touch框架提供一個UIImageView的分類,加載圖片并進行緩存處理。
2)異步圖像下載
3)異步存儲器+具備自動緩存過期處理的磁盤映像緩存
4)支持GIF播放
5)支持WebP格式
6)背景圖像解壓縮
7)保證同一個url圖片資源不被多次下載
8)保證錯誤url不被反復嘗試下載
9)保證不會阻塞主線程
10)高性能
11)使用GCD和ARC
12)支持Arm64架構

注 :SDWebimage3.0版本并沒有完全向后兼容2.0版本且要求的最低配置為iOS5.1.1版本。如果你需要在iOS5.0以前的版本使用,那么請您使用2.0版本。

How is SDWebImage better than X?

·AFNetworking已經提供UIImageView相似的功能,還有必要使用SDWebimage嗎?

Who Use It(誰用)

查看 哪些應用 使用了SDWebImage框架,添加你的應用到列表。

How To Use(如何用)

查看api文檔 CocoaDocs - SDWebImage

TableView加載圖片使用UIImageView+WebCache分類

只需要包含UIImageView+WebCache.h頭文件,并在tableView的數據源方法tableView:cellForRowAtIndexPath: 中調用sd_setImageWithURL:placeholderImage:方法即可。異步下載和緩存處理這一切都將會自動為你處理。

#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 sd_setImageWithURL: method to load the web image
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

使用Blocks

使用block,你將能夠得到圖片的下載進度并獲知圖片是否下載成功或者失敗:

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                ... completion code here ...
                             }];

注意 :如果圖像請求在完成前被取消了,那么成功和失敗的block塊將都不會被調用。

使用SDWebImageManager

UIImageView+WebCache分類背后調用的是SDWebImageManager類的方法,負責圖像的異步下載和緩存處理。你可以直接使用這個類來下載圖片和進行緩存處理。這有一個如何使用SDWebImageManager的簡單示例:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imageURL
                      options:0
                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                         // progression tracking code
                     }
                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                         if (image) {
                             // do something with image
                         }
                     }];

單獨異步下載圖片

它也可以獨立使用異步圖片下載:

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
                         options:0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            // progression tracking code
                        }
                       completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                            if (image && finished) {
                                // do something with image
                            }
                        }];

單獨異步緩存圖片

也可以單獨使用基于圖像的高速異步緩存處理。SDImagecache提供內存高速緩存和可選的磁盤高速緩存。磁盤高速緩存寫入操作是異步的,所以不需要在用戶界面添加不必要的延遲。

為了方便,SDImageCache類提供了一個單一的實例,但如果你想自定義緩存空間,那么可以創建自己的實例。

您可以使用 queryDiskCacheForKey:done: 方法查找緩存。如果該方法返回nil,則說明當前image沒有緩存。你需要負責下載和進行緩存處理。圖像緩存的key通常為該圖像的URL。

SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
    // image is not nil if image was found
}];

默認情況下,如果對應圖片的內存緩存不存在,那么SDImageCache將查找磁盤緩存。可以通過調用 imageFromMemoryCacheForKey: 方法來阻止。

你可以調用 storeImage:forKey: method: 方法保存圖片到緩存。

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

默認情況下,圖像將被進行內存緩存和磁盤緩存(異步)。如果你只想要內存緩存,那么可以使用storeImage:forKey:toDisk:方法,第三個參數傳NO即可。

使用緩存key篩選器

有時,你可能會因為URL的一部分是動態的而不希望使用URL作為圖像緩存的key。 SDWebImageManager提供了一種方式來設置,輸入URL輸出對應的字符串。下面的示例在應用程序的委托中設置一個過濾器,在使用它的緩存鍵之前將從URL中刪除任何查詢字符串

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) {
        url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
        return [url absoluteString];
    };

    // Your app init code...
    return YES;
}

常見問題

使用動態圖像大小的UITableViewCell

UITableViewCell通過第一個單元格設置的圖像決定圖像的尺寸。如果你加載的網絡圖片和占位符圖像尺寸不一致,那么您可能會遇到奇怪的變形比例問題。下面的文章給出了解決此問題的方法:

http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/

處理圖像刷新

SDWebimage默認情況下做了很好的緩存處理。它忽略通過HTTP服務器返回的所有類型的緩存控制頭,并且沒有時間限制地緩存返回的圖像

它意味著你的url指向的圖片是一成不變的。更好的做法是如果url指向的圖片發生了改變,那么圖片也應該變化。

在這種情況下,你可以使用 SDWebImageRefreshCached 標志。這將略微降低性能,但會尊重HTTP緩存控制頭:

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.非死book.com/olivier.poitrey/picture"]
                 placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                          options:SDWebImageRefreshCached];

添加進度指示器

參考: https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage

安裝

有三種方法把SDWebImage安裝到您的項目中:

1)使用Cocoapods
2)復制所有文件到您的項目
3)作為靜態庫導入項目
  • Posted by 博客園·文頂頂
  • 聯系作者 簡書· 文頂頂 新浪微博·文頂頂_iOS
  • 原創文章,版權聲明:自由轉載-非商用-非衍生-保持署名 | 小碼哥教育·文頂頂

來自: http://www.cnblogs.com/wendingding/p/5110732.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!