iOS網絡圖片尺寸適配

一.前言:

  • 1.iOS開發中,常碰到網絡圖片需要做尺寸適配(使顯示出來的圖片不變形)
  • 2.最好的解決方案是:后臺把圖片的分辨率拼接在圖片的URL地址中,我們截取獲得分辨率,從而根據寬高比,來適配imageView尺寸.
  • 3.但往往有些時候由于各種原因,圖片分辨率后臺那邊加不上去,沒辦法只好我們自己來解決了.
  • 4.正好前段時間有這個需求,故特意寫了一個工具XHWebImageAutoSize,來處理這個問題.
  • 5.項目/代碼地址:見篇末

二.效果:

Demo1.png
Demo2.png
Demo3.png

三.XHWebImageAutoSize特性:

  • 1.異步緩存網絡圖片尺寸,優先從緩存中獲取圖片尺寸.
  • 2.UITableView,UICollectionView動態刷新UI.

四.工作原理:

  • 1.XHWebImageAutoSize的工作原理很簡單,就是在首次加載網絡圖片后,緩存該圖片的尺寸,動態刷新UI,下次直接調用緩存的尺寸.

五.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相關

/**
 Reload tableView

 @param url imageURL
 */
-(void)xh_reloadDataForURL:(NSURL *)url;

  • 3.collectionView reload相關
/**
 Reload collectionView
 
 @param url imageURL
 */
-(void)xh_reloadDataForURL:(NSURL *)url;

六.使用方法:

  • 此處以在UITableView中使用,UITableViewCell上僅有一個UIImageView為例,其他示例詳見->>>DEMO
   
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = self.dataArray[indexPath.row];
    /**
     *  參數1:圖片URL
     *  參數2:imageView 寬度
     *  參數3:預估高度(此高度僅在圖片尚未加載出來前起作用,不影響真實高度)
     */
   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;
}

七.小結:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容