一.前言:
- 1.iOS開(kāi)發(fā)中,常碰到網(wǎng)絡(luò)圖片需要做尺寸適配(使顯示出來(lái)的圖片不變形)
- 2.最好的解決方案是:后臺(tái)把圖片的分辨率拼接在圖片的URL地址中,我們截取獲得分辨率,從而根據(jù)寬高比,來(lái)適配imageView尺寸.
- 3.但往往有些時(shí)候由于各種原因,圖片分辨率后臺(tái)那邊加不上去,沒(méi)辦法只好我們自己來(lái)解決了.
- 4.正好前段時(shí)間有這個(gè)需求,故特意寫(xiě)了一個(gè)工具XHWebImageAutoSize,來(lái)處理這個(gè)問(wèn)題.
- 5.項(xiàng)目/代碼地址:見(jiàn)篇末
二.效果:
Demo1.png
Demo2.png
Demo3.png
三.XHWebImageAutoSize特性:
- 1.異步緩存網(wǎng)絡(luò)圖片尺寸,優(yōu)先從緩存中獲取圖片尺寸.
- 2.UITableView,UICollectionView動(dòng)態(tài)刷新UI.
四.工作原理:
- 1.XHWebImageAutoSize的工作原理很簡(jiǎn)單,就是在首次加載網(wǎng)絡(luò)圖片后,緩存該圖片的尺寸,動(dòng)態(tài)刷新UI,下次直接調(diào)用緩存的尺寸.
五.API:
/**
* Get image height
*
* @param url imageURL
* @param layoutWidth layoutWidth
* @param estimateHeight estimateHeight(default 100)
*
* @return imageHeight
*/
+(CGFloat)imageHeightForURL:(NSURL *)url layoutWidth:(CGFloat)layoutWidth estimateHeight:(CGFloat )estimateHeight;
/**
* Get image size from cache,query the disk cache synchronously after checking the memory cache
*
* @param url imageURL
*
* @return imageSize
*/
+(CGSize )imageSizeFromCacheForURL:(NSURL *)url;
/**
* Store an imageSize into memory and disk cache
*
* @param image image
* @param url imageURL
* @param completedBlock An block that should be executed after the imageSize has been saved (optional)
*/
+(void)storeImageSize:(UIImage *)image forURL:(NSURL *)url completed:(XHWebImageAutoSizeCacheCompletionBlock)completedBlock;
/**
* Get reload state from cache,query the disk cache synchronously after checking the memory cache
*
* @param url imageURL
*
* @return reloadState
*/
+(BOOL)reloadStateFromCacheForURL:(NSURL *)url;
/**
* Store an reloadState into memory and disk cache
*
* @param state reloadState
* @param url imageURL
* @param completedBlock An block that should be executed after the reloadState has been saved (optional)
*/
+(void)storeReloadState:(BOOL)state forURL:(NSURL *)url completed:(XHWebImageAutoSizeCacheCompletionBlock)completedBlock;
- 2.tableView reload相關(guān)
/**
Reload tableView
@param url imageURL
*/
-(void)xh_reloadDataForURL:(NSURL *)url;
- 3.collectionView reload相關(guān)
/**
Reload collectionView
@param url imageURL
*/
-(void)xh_reloadDataForURL:(NSURL *)url;
六.使用方法:
- 此處以在UITableView中使用,UITableViewCell上僅有一個(gè)UIImageView為例,其他示例詳見(jiàn)->>>DEMO
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *url = self.dataArray[indexPath.row];
/**
* 參數(shù)1:圖片URL
* 參數(shù)2:imageView 寬度
* 參數(shù)3:預(yù)估高度(此高度僅在圖片尚未加載出來(lái)前起作用,不影響真實(shí)高度)
*/
return [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:url] layoutWidth:[UIScreen mainScreen].bounds.size.width-16 estimateHeight:200];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DemoVC1Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell){
cell = [[DemoVC1Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSString *url = self.dataArray[indexPath.row];
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
/** 緩存image size */
[XHWebImageAutoSize storeImageSize:image forURL:imageURL completed:^(BOOL result) {
/** reload */
if(result) [tableView xh_reloadDataForURL:imageURL];
}];
}];
return cell;
}