1 設置imageView的圖片 (內存緩存&磁盤緩存)
數組
1 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"placehoder"]];
2 設置圖片另外幾種方法緩存
1 /*
2 第一個參數:要下載圖片的url地址 3 第二個參數:設置該imageView的佔位圖片 4 第三個參數:傳一個枚舉值,告訴程序你下載圖片的策略是什麼 5 第一個block塊:獲取當前圖片數據的下載進度 6 receivedSize:已經下載完成的數據大小 7 expectedSize:該文件的數據總大小 8 第二個block塊:當圖片下載完成以後執行該block中的代碼 9 image:下載獲得的圖片數據 10 error:下載出現的錯誤信息 11 SDImageCacheType:圖片的緩存策略(不緩存,內存緩存,沙盒緩存) 12 imageURL:下載的圖片的url地址 13 */
14 [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img5.duitang.com/uploads/item/201409/02/20140902150244_nMMEj.jpeg"] placeholderImage:[UIImage imageNamed:@"Snip20160111_304"] options:(kNilOptions) progress:^(NSInteger receivedSize, NSInteger expectedSize) { 15 NSLog(@"%f",1.0 * receivedSize/expectedSize); 16 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {17 switch (cacheType) { 18 case SDImageCacheTypeNone: 19 NSLog(@"沒有使用緩存,圖片是直接下載的"); 20 break; 21 case SDImageCacheTypeDisk: 22 NSLog(@"磁盤緩存"); 23 break; 24 case SDImageCacheTypeMemory: 25 NSLog(@"內存緩存"); 26 break; 27 default: 28 break; 29 } 30 }];
1 //若是不須要佔位圖片,能夠用以下代碼 2 [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://img5.duitang.com/uploads/item/201409/02/20140902150244_nMMEj.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { 3 NSLog(@"%f",1.0 * receivedSize/expectedSize); 4 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 5 self.imageView.image = image; 6 switch (cacheType) { 7 case SDImageCacheTypeNone: 8 NSLog(@"沒有使用緩存,圖片是直接下載的"); 9 break; 10 case SDImageCacheTypeDisk: 11 NSLog(@"磁盤緩存"); 12 break; 13 case SDImageCacheTypeMemory: 14 NSLog(@"內存緩存"); 15 break; 16 default: 17 break; 18 } 19 }];
1 //注意點:completed回調是在子線程處理的 2 //內部並不會作緩存處理 3 4 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://img.qqbody.com/uploads/allimg/201503/20150331222879.gif"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { 5 NSLog(@"%f",1.0 * receivedSize/expectedSize); 6 7 } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { 8 9 //線程間通訊 10 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 11 self.imageView.image = [UIImage sd_animatedGIFWithData:data]; 12 }]; 13 }];
3 系統級的內存警告如何處理安全
1 //取消當前正在進行的全部下載操做
2 [[SDWebImageManager sharedManager] cancelAll]; 3
4 //清除緩存數據 5 //cleanDisk:刪除過時的文件數據,計算當前未過時的已經下載的文件數據的大小,若是發現該數據大小大於咱們設置的最大緩存數據大小,那麼程序內部會按照按文件數據緩存的時間從遠到近刪除,知道小於最大緩存數據爲止。
6 [[SDWebImageManager sharedManager].imageCache cleanDisk]; 7
8 //clearMemory:直接刪除文件,從新建立新的文件夾
9 [[SDWebImageManager sharedManager].imageCache clearMemory];
1 + (UIImage *)sd_animatedGIFNamed:(NSString *)name 2 + (UIImage *)sd_animatedGIFWithData:(NSData *)data
b.內部實現網絡
1 /*
2 5-1 把用戶傳入的gif圖片->NSData 3 5-2 根據該Data建立一個圖片數據源(NSData->CFImageSourceRef) 4 5-3 計算該數據源中一共有多少幀,把每一幀數據取出來放到圖片數組中 5 5-4 根據獲得的數組+計算的動畫時間 == 可動畫的image 6 [UIImage animatedImageWithImages:images duration:duration]; 7 */
8 //若是圖片幀數小於等於1,那麼就直接把二進制數據轉換爲圖片,並返回圖片
9 if (count <= 1) { 10 animatedImage = [[UIImage alloc] initWithData:data]; 11 }else { 12 //建立可變的空的圖片數組
13 NSMutableArray *images = [NSMutableArray array]; 14 //初始化動畫播放時間爲0
15 NSTimeInterval duration = 0.0f; 16 // 遍歷而且提取全部的動畫幀
17 for (size_t i = 0; i < count; i++) { 18 CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); 19 // 累加動畫時長
20 duration += [self sd_frameDurationAtIndex:i source:source]; 21 // 將圖像添加到動畫數組
22 [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; 23 //釋放操做
24 CGImageRelease(image); 25 } 26 //計算動畫時間
27 if (!duration) { 28 duration = (1.0f / 10.0f) * count; 29 } 30 // 創建可動畫圖像
31 animatedImage = [UIImage animatedImageWithImages:images duration:duration]; 32 }
6 如何判斷當前圖片類型,只判斷圖片二進制數據的第一個字節多線程
+ (NSString *)sd_contentTypeForImageData:(NSData *)data;
- (void)setObject:(ObjectType)obj forKey:(KeyType)key;//在緩存中設置指定鍵名對應的值,0成本
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;//在緩存中設置指定鍵名對應的值,而且指定該鍵值對的成本,用於計算記錄在緩存中的全部對象的總成本,出現內存警告或者超出緩存總成本上限的時候,緩存會開啓一個回收過程,刪除部分元素
- (void)removeObjectForKey:(KeyType)key;//刪除緩存中指定鍵名的對象
- (void)removeAllObjects;//刪除緩存中全部的對象