SDWebImage 新版接口使用方法


最新的庫結構如下

初次導入,如果你的 Xcode 不是最新的,可能會遇到問題,解決版本

(1) 下載了SDWebImage.framework,然后拖到了工程中,然后:

#import "UIImageView+WebCache.h"

死活import不進啊,提示"UIImageView+WebCache.h file not found"

解決方法,最新版本的SDWebImage需要import的不是上面那一句了,需要:

#import<SDWebImage/UIImageView+WebCache.h>

然后build,一切OK。

(2) 另外一個問題就是:

Symbol Not Found: _objc_storeStrong

因為在no arc項目中引用了arc 的SDWebImage,更改一下: Build Settings->Other Linker Flags 添加 -fobjc-arc.

 

---------------------------------------------------------------------------

第一步,在需要的地方導入頭文件

#import<SDWebImage/UIImageView+WebCache.h>

第二步,調用sd_setImageWithURL:方法緩存圖片,注意,這就是新版本的新方法,舊方法是 setImageWithURL : 。下面將幾個方法都介紹一下。

//1 圖片緩存的基本代碼,就是這么簡單
    [self.image1 sd_setImageWithURL:imagePath1];

//2 用block 可以在圖片加載完成之后做些事情 [self.image2 sd_setImageWithURL:imagePath2 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSLog(@"這里可以在圖片加載完成之后做些事情"); }];

//3 給一張默認圖片,先使用默認圖片,當圖片加載完成后再替換 [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];
 
//4 使用默認圖片,而且用block 在完成后做一些事情 [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSLog(@"圖片加載完成后做的事情"); }];

//5 options 選擇方式  [self.image1 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"] options:SDWebImageRetryFailed];
 
 

除了帶options選項的方法,其他的方法都是綜合存儲,也就是內存緩存和磁盤緩存結合的方式,如果你只需要內存緩存,那么在options這里選擇SDWebImageCacheMemoryOnly就可以了。

 

當你調用 setImageWithURL: 方法的時候,他會自動去給你干這么多事,當你需要在某一具體時刻做事情的時候,你可以覆蓋這些方法。比如在下載某個圖片的過程中要響應一個事件,就覆蓋這個方法:

//覆蓋方法,指哪打哪,這個方法是下載imagePath2的時候響應
  SDWebImageManager *manager = [SDWebImageManager sharedManager];
  [manager downloadImageWithURL:imagePath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    NSLog(@"顯示當前進度");
  } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    NSLog(@"下載完成");
  }];

 

--------------------------

 

如果想取出 SD 緩存在本地的圖片,可以使用下面代碼

NSURL *url = [NSURL URLWithString:[model.content objectForKey:@"image"]];
            //請求網絡地址數據的同步方法
            //因為這個方法在子線程(全局隊列)中執行,所以不需要考慮死線程的問題
            SDWebImageManager *manager = [SDWebImageManager sharedManager];
             [manager diskImageExistsForURL:url];
            if ([manager diskImageExistsForURL:url]) {
                LOG(@"11111")
                self.speakImage = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
            }else{
                LOG(@"3333333")
                NSData *data = [NSData dataWithContentsOfURL:url];
                self.speakImage = [UIImage imageWithData:data];
            }

--------------------------

SDWebImage 在從 URL 獲取超過 2 m 的圖片時,會造成在內存小的機器上閃退,解決方法,重寫一下內置的方法

如上圖中的 UIImage+MultiFormat.m 中

+ (UIImage *)sd_imageWithData:(NSData *)data {
    UIImage *image;
    NSString *imageContentType = [NSData sd_contentTypeForImageData:data];
    if ([imageContentType isEqualToString:@"image/gif"]) {
        image = [UIImage sd_animatedGIFWithData:data];
    }
#ifdef SD_WEBP
    else if ([imageContentType isEqualToString:@"image/webp"])
    {
        image = [UIImage sd_imageWithWebPData:data];
    }
#endif
    else {
        image = [[UIImage alloc] initWithData:data];
        UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];
        if (orientation != UIImageOrientationUp) {
            image = [UIImage imageWithCGImage:image.CGImage
                                        scale:image.scale
                                  orientation:orientation];
        }
    }

//------------------------------------------------------------
// 這里是修改的代碼,根據不同屏幕尺寸對圖片進行裁切,使內存中圖片占用較小
//------------------------------------------------------------
float width = 0.0; switch ((int)[UIScreen mainScreen].bounds.size.width) { case 320: width = 640.0; break; case 375: width = 750.0; break; case 414: width = 1242.0; break; default: width = 640.0; break; } //對網絡圖片進行縮放 float scaleSize = width/image.size.width; if (scaleSize < 1) { UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } return image; }

 

-----------------------------------------------------------------------------------

SDWebImageDownloader

這個下載器改版很大,現在里面就一個方法,就是發起前往下載圖片的函數:

    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageUrl options:SDWebImageDownloaderProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize) { NSLog(@"%u %lld",receivedSize,expectedSize); } completed:^(UIImage *aImage, NSData *data, NSError *error, BOOL finished) { //self.image = aImage; NSLog(@"成功了:%d",UIImageJPEGRepresentation(aImage, 1).length); }]; 

 

進度Block返回的信息有兩個參數:1:下載進度. 2:文件大小.

 

我這里解釋一下里面的options枚舉的作用

SDWebImageDownloaderOptions

1:SDWebImageDownloaderLowPriority

  這個屬於默認的使用模式了,前往下載,返回進度Block信息,完成時調用completedBlock

2:SDWebImageDownloaderProgressiveDownload

  這個是新版本加的功能,設置后,在返回進度Block的同時,返回completedBlock,里面的UIImage就是當前下載時的圖片,可以實現將圖片一點點顯示出來的功能.

 

那么SDWebImageDownloader 在新版本的中的分工很明確,只做一件事情,那就是下載,只是下載而已,所以,下載成功以后的圖片資源是不會自動緩存的.

如果要緩存,這里需要使用到

SDImageCache

SDImageCacheType 當下載請求結束時,通過這個枚舉來告知圖片的來源

1:SDImageCacheTypeNone     網絡

2:SDImageCacheTypeDisk     設備硬盤

3:SDImageCacheTypeMemory   內存

 

將上面兩步驟的需求合並可直接使用

SDWebImageManager

新版本的管理類的改動也相當大,而且就一個方法了,如下:

 

     [[SDWebImageManager sharedManager] downloadWithURL:imageUrl options:SDWebImageLowPriority progress:^(NSUInteger receivedSize, longlong ,UIImageJPEGRepresentation(aImage, 1).length);  
    }];  

 

這樣發起的下載請求,就會自動緩存圖片資源了.


那么任何發起的下載請求,都會返回一個代理. 這個寫法很神奇,值得深究啊.

也就是:SDWebImageOperation 將這個委托緩存一下,做什么用呢?一件事情,取消下載請求.


 

這邊文章介紹了整個SDWebImage的運作原理, 真TMD 的復雜

http://blog.csdn.net/uxyheaven/article/details/7909373

 

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM