XHWebImageAutoSize 1.1.2

XHWebImageAutoSize 1.1.2

测试已测试
语言语言 Objective-CObjective C
许可协议 MIT
发布最后发布2018年1月

CoderZhuXH 维护。



  • 作者
  • 朱晓辉

XHWebImageAutoSize

网络图片尺寸/高度自适应解决方案

AppVeyor
Version Status
Support
Pod Platform
Pod License

==============

前言

  • 1.在 iOS 开发中,常遇到需要自适应尺寸的网络图片(使显示出的图片不变形)的场景。
  • 2.最好的解决方案是在图片的 URL 地址中拼接分辨率,然后根据宽高比来适配 imageView 的尺寸。
  • 3.但有时后台提供的图片 URL 地址中没有分辨率,且不允许添加时,我们就只能自己解决了。
  • XHWebImageAutoSize 就是用来解决这个问题。

特性

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

技术交流群(群号:537476189)。

效果

使用方法

1.此处以在 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  */
            if(result)  [tableView  xh_reloadDataForURL:imageURL];
        }];
        
    }];
    return cell;
}

API

  • 1.获取图片高度/尺寸及缓存相关。
/**
 *   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 tableView

 @param url imageURL
 */
-(void)xh_reloadDataForURL:(NSURL *)url;
  • 3collectionView 重新加载。
/**
 Reload collectionView
 
 @param url imageURL
 */
-(void)xh_reloadDataForURL:(NSURL *)url;

安装

1.手动添加

  • 1.将 XHWebImageAutoSize 文件夹添加到工程目录中。
  • 2.导入 XHWebImageAutoSize.h。

2.CocoaPods

  • 1.在 Podfile 中添加 pod 'XHWebImageAutoSize'。
  • 2.执行 pod install 或 pod update。
  • 3.导入 XHWebImageAutoSize.h。

提示

  • 1.如果发现 pod search XHWebImageAutoSize 搜索出的不是最新版本,需要在终端执行 cd ~/desktop 返回桌面,然后执行 pod setup 命令更新本地 spec 缓存(需要几分钟),然后再搜索。
  • 2.如果你发现执行 pod install 后,导入的不是最新版本,请删除 Podfile.lock 文件,再执行一次 pod install。
  • 3.如果在使用过程中遇到问题,希望你能 Issues 我,谢谢(或者尝试下载最新的代码看看问题是否已修复)。

系统要求

  • 该项目最低支持 iOS 7.0 和 Xcode 7.0。

许可协议

XHWebImageAutoSize 使用 MIT 许可协议,详情见 LICENSE 文件。