SDWebImage學習

一、一些使用文件的翻譯


HowToUse.md

在tableView里使用UIImageView+WebCache 這個類別
  • 只需要import UIImageView+WebCache.h 這個頭文件,并且在tableView:cellForRowAtIndexPath: UITableViewDataSource方法里使用sd_setImageWithURL:placeholderImage:
    我們會為你處理好所有問題,包括異步下載和緩存管理
    1. objective-c
      (<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;
}
  • 2.swift
import SDWebImage
...
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    static let myIdentifier = "MyIdentifier"
    let cell = tableView.dequeueReusableCellWithIdentifier(myIdentifier, forIndexPath: indexPath) as UITableViewCell

    cell.imageView.sd_setImageWithURL(imageUrl, placeholderImage:placeholderImage)
    return cell
使用block

在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 ...
                             }];

注意:如果你的圖片下載請求在圖片下載完成前取消了,那么它既不會進入成功回調(diào)的block,也不會進入失敗回調(diào)的block
Note: neither your success nor failure block will be call if your image request is canceled before completion.

使用SDWebImageManager

SDWebImageManagerUIImageView(WebCache)的子類,它把異步下載和圖片緩存結(jié)合起來,從web上下載的緩存到本地的圖片,在別的情況下用的的時候,你可以直接使用他。
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).

下面這個例子說明如何使用SDWebImageManager
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager loadImageWithURL: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維護內(nèi)存緩存和可選磁盤緩存。執(zhí)行磁盤高速緩存寫入操作

  • 異步,因此它不會向UI添加不必要的延遲。

  • 為了方便,SDImageCache類提供了一個單例實例,但是您可以創(chuàng)建自己的實例

  • 實例化這個類如果你想要創(chuàng)建單獨的高速緩存命名空間。

  • 要查找緩存,您可以使用queryDiskCacheForKey:done:方法。如果該方法返回nil,則意味著緩存
    目前不擁有該圖片。因此,您負責生成和緩存它。緩存
    key是要緩存的圖像的應(yīng)用程序唯一標識符。它一般情況下來說是嚴格URL圖片

objective-c:
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
    // image is not nil if image was found
}];
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
  • 默認情況下,如果在內(nèi)存緩存中找不到映像,SDImageCache將查找磁盤緩存。
    你可以通過調(diào)用替代方法imageFromMemoryCacheForKey:來防止這種情況的發(fā)生。
objective-c
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

默認情況下,圖片將被保存到內(nèi)存中,同時也保存在磁盤上(他們是異步執(zhí)行的)。如果,你只想讓他保存在磁盤上,你可以替代方法storeImage:forKey:toDisk:,第三個參數(shù)填:NO

使用緩存鍵過濾器

有時,您可能不想使用圖片網(wǎng)址作為緩存鍵,因為網(wǎng)址的一部分是動態(tài)的(即:用于訪問控制目的)。 SDWebImageManager提供了一種設(shè)置緩存密鑰過濾器的方法
以NSURL作為輸入,并輸出高速緩存密鑰NSString。

以下示例在應(yīng)用程序委托中設(shè)置一個過濾器,它將從中刪除任何查詢字符串在使用它之前的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;
}

ManualInstallation.md

為獲得所有文件的使用權(quán)限,你需要clone他
git clone --recursive https://github.com/rs/SDWebImage.git
檢查一下依賴庫和描述文件
添加依賴庫
  • 在app的target設(shè)置那里,找到"Build Phases"選項,并且打開"Link Binary With Libraries"
  • 點擊"+" 按鈕,并且選擇"ImageIO.framework"選項,當你的項目需要漸進式下載功能的時候
添加連接標識(Add Linker Flag)
  • 在app的target設(shè)置那里,找到"Build Settings"區(qū)域,找到"Other Linker Flags"設(shè)置項,添加"-ObjC"標識


    Other Linker Flags
    Other Linker Flags

    或者是,如果可擴展框架(Parse, RestKit or opencv2)導致編譯錯誤,用下列方式代替" -ObjC flag"

-force_load SDWebImage.framework/Versions/Current/SDWebImage

如果你用的是Cocoa Pods,并且有同樣的問題,用下列方式代替" -ObjC flag"

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

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

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