iOS网络图片尺寸适配

2016-12-05 朱晓辉 更多博文 » 博客 » GitHub »

原文链接 http://it7090.com/2016/12/05/iOS%E7%BD%91%E7%BB%9C%E5%9B%BE%E7%89%87%E5%B0%BA%E5%AF%B8%E9%80%82%E9%85%8D/
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


iOS开发中,常碰到网络图片需要做尺寸适配(使显示出来的图片不变形) ,最好的解决方案是:后台把图片的分辨率拼接在图片的URL地址中,我们截取获得分辨率,从而根据宽高比,来适配imageView尺寸. ,但当后台所给图片URL地址中没有分辨率,他又不肯加时,只好我们自己来解决了. ,正好前段时间有这个需求,故特意写了一个工具XHWebImageAutoSize,来处理这个问题.

效果:

Demo2.png Demo3.png

XHWebImageAutoSize特性:

  • 1.异步缓存网络图片尺寸,优先从缓存中获取图片尺寸.
  • 2.UITableView,UICollectionView动态刷新UI.

工作原理:

  • 1.XHWebImageAutoSize的工作原理很简单,就是在首次加载网络图片后,缓存该图片的尺寸,下次直接调用缓存的尺寸.

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 row
 *
 *  @param indexPath indexPath
 *  @param url        imageURL
 */
-(void)xh_reloadRowAtIndexPath:(NSIndexPath *)indexPath forURL:(NSURL *)url;

/**
 *  Reload row withRowAnimation
 *
 *  @param indexPath indexPath
 *  @param animation UITableViewRowAnimation
 *  @param url       imageURL
 */
-(void)xh_reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation forURL:(NSURL *)url;

  • 3.collectionView reload相关
/**
 *  Reload item
 *
 *  @param indexPath indexPath
 *  @param url        imageURL
 */
-(void)xh_reloadItemAtIndexPath:(NSIndexPath *)indexPath forURL:(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];
    //加载网络图片使用SDWebImage
    [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 cell
            */
          if(result)  [tableView  xh_reloadRowAtIndexPath:indexPath forURL:imageURL];

        }];

        }];

    }];
    return cell;
}

小结: