#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; }
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).前端
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/userManager/0.png"]; //SDWebImageManager管理器繼承與UIImageView+WedCache。已異步的方式進行下載圖片到緩衝區。 SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"%ld,%ld",receivedSize,expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { //爲imageView設置圖片爲下載的圖片 [self.imageView setImage:image]; //打印圖片的來源 NSLog(@"%@",imageURL); }];
It's also possible to use the async image downloader independently:web
[SDWebImageDownloader.sharedDownloader 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 } }];
在本身的案例中使用SDWebImage代碼段:緩存
添加自定義的只讀緩存路徑,其中這是可選的,若是沒有指定,系統會默認 #import "SDWebImage/SDImageCache.h" #import "AppDelegate.h" ...... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //添加自定義的只讀緩存路徑:可選的 NSString *bundledPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CustomPathImages"]; [[SDImageCache sharedImageCache] addReadOnlyCachePath:bundledPath]; ...... }
在使用緩存時,若是內存不夠使用時,須要進行清理以釋放內存。app
#import "MasterViewController.h" #import "SDWebImage/UIImageView+WebCache.h" ...... //清理緩存, - (void)flushCache { /** * 一、SDImageCache是怎麼作數據管理的? SDImageCache分兩個部分,一個是內存層面的,一個是硬盤層面的。 內存層面的至關是個緩存器,以Key-Value的形式存儲圖片。 */ //清理內存緩存 [SDWebImageManager.sharedManager.imageCache clearMemory]; //清理硬盤緩存 [SDWebImageManager.sharedManager.imageCache clearDisk]; } //支持屏幕旋轉 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }
......
//爲了顯示大圖片,替將鏈接中small替換成source;dom
NSString *largeImageURL = [[_objects objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];異步
......async
儀表器的使用方法。ide
#import "DetailViewController.h" #import "SDWebImage/UIImageView+WebCache.h" ...... - (void)configureView { if (self.imageURL) { //建立儀表器 __block UIActivityIndicatorView *activityIndicator; //爲防止循環引用問題,因此在block中使用imageView時要設置成弱引用,imageView中使用block,在block中若是再調用imageView時,不設置爲弱引用,會出現循環引用問題。 __weak UIImageView *weakImageView = self.imageView; [self.imageView sd_setImageWithURL:self.imageURL placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) { //檢測是否已經存在啦!,若是沒有在新建立出一個 if (!activityIndicator) { //將新建立的儀表器添加到imageView視圖中 [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; activityIndicator.center = weakImageView.center; //開啓動畫,轉吧!小宇宙 [activityIndicator startAnimating]; } } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){ //將儀表器從imageView視圖中進行刪除 [activityIndicator removeFromSuperview]; activityIndicator = nil; }]; } } ......
在Cell中使用異步圖片下載。函數
#import "MasterViewController.h" #import "SDWebImage/UIImageView+WebCache.h" ...... /** * 參數解析: 1.獲取圖片的路徑 2.設置佔位圖片 3.options選項:當indexPath.row爲0時,執行 SDWebImageRefreshCached:刷新緩存 */ [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; ......
在上段代碼中,有以下兩行代碼: fetch
__weak UIImageView *weakImageView = self.imageView;
__block UIActivityIndicatorView *activityIndicator;
其中第一條爲了不循環引用問題,加上了__weak,第二條使用了block。如今補充一下循環引用、block的知識點:
循環引用
全部的引用計數系統,都存在循環應用的問題。例以下面的引用關係:
1)對象a建立並引用到了對象b.
2)對象b建立並引用到了對象c.
3)對象c建立並引用到了對象b.
這時候b和c的引用計數分別是2和1。當a再也不使用b,調用release釋放對b的全部權,由於c還引用了b,因此b的引用計數爲1,
b不會被釋放。b不釋放,c的引用計數就是1,c也不會被釋放。今後,b和c永遠留在內存中。這種狀況,必須打斷循環引用
經過其餘規則來維護引用關係。好比,咱們常見的delegate每每是assign方式的屬性而不是retain方式 的屬性,
賦值不會增長引用計數,就是爲了防止delegation兩端產生沒必要要的循環引用。若是一個UITableViewController對象a
經過retain獲取了UITableView對象b的全部權,這個UITableView對象b的delegate又是a,若是這個delegate是retain方式的,
那基本上就沒有機會釋放這兩個對象了。本身在設計使用delegate模式時,也要注意這點。
Block
是能夠獲取其餘函數局部變量的匿名函數,其不但方便開發,而且能夠大幅提升應用的執行效率(多核心CPU可直接處理Block指令),若是但願進行修改局部變量的值,能夠在定義局部變量以前使用__block。