iOS tableView的图片缓存异步加载
发布日期:2021-05-24 08:24:27 浏览次数:18 分类:精选文章

本文共 6783 字,大约阅读时间需要 22 分钟。

建立UIScrollView和UITableView的ViewController

1. 创建UIViewController类

首先,我们需要创建一个UIViewController类,并将其设置为UIScrollView和UITableView的代理。

#import 
@protocol ICTableViewDelegate -(void) cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray; @end @interface ICTableViewController : UIViewController
@property (nonatomic, strong) id
delegate; @property (nonatomic, strong):UITableView *tableView; @property (nonatomic, strong) NSMutableArray *tableDataArray; @end

2. 实现图片缓存的逻辑

我们需要在.loadCellImage方法中实现图片缓存和加载的逻辑。这将确保图片能够在视图中展示,同时避免重复请求

-(void) loadCellImage  {      NSArray *indexPathsForLoad = [self.tableView indexPathsForVisibleRows];      for (NSIndexPath *item in indexPathsForLoad) {          NSInteger rowNumberForCell = item.row;          if (rowNumberForCell >= [self.tableDataArray count]) {              return;          }          NSString *imageStr = self.tableDataArray[rowNumberForCell][@"photoFileUrl"];          if ([imageStr length] == 0) {              continue;          }          NSArray *photoUrl = [imageStr componentsSeparatedByString:MULTI_FILES_SEPARATOR];          for (NSInteger i = 0; i < photoUrl.count; i++) {              NSString *url = [photoUrl[i] stringByAppendingString:@".temp"];              NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[                  @"Library/Caches/" stringByAppendingPathComponent:url              ]];              NSData *imageData = [NSData dataWithContentsOfFile:imageDataPath];              if (imageData) {                  UIImage *image = [UIImage imageWithData:imageData];                  if (!image) {                      continue;                  }                  UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))];                  if (thumbImg) {                      //设置图片到相应的控件中                      [photoBtn.imgView setImage:thumbImg];                      [cell.contentView addSubview:photoBtn];                  }              }          }      }  }

3. 子类化UIViewController并实现代理方法

在子类化UIViewController中,设置单元格内容并实现ICTableViewDelegate代理方法

@interface CustomPhotoTableViewCell : UITableViewCell  @property (nonatomic, strong) IBOutlet UIButton *photoBtn;  @property (nonatomic, strong) UILabel *urlLabel;  @property (nonatomic, strong) UIImageView *imgView;  @end  @interface CustomPhotoTableViewController : UIViewController 
@property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *tableDataArray; @end @implementation CustomPhotoTableViewController -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomPhotoTableViewCell *cell = (CustomPhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier: [UITableViewCell 蒂PropertyValue:nil]]; if (!cell) { [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"]; } NSString *imageName = [url stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[ @"Library/Caches/" stringByAppendingPathComponent:imageName ]]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]]; UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))]; if (thumbImg) { [cell.imgView setImage:thumbImg]; } else { [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]]; } [cell.photoBtn/fileFullName setText:url]; return cell; } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 单元格点击事件处理 } - (void) cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray { CustomPhotoTableViewCell *cell = (CustomPhotoTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; if (imageArray.count > 0) { for (NSInteger i = 0; i < imageArray.count; i++) { if (is_iOS8_or_later) { CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i + 1) * 10]; if (photoBtn) { UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:(CGSizeMake(60, 40))]; [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } else { [tableView beginUpdates]; [cell(Layout setFrame:cell(Layout).frame); [tableView endUpdates]; } } else { CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i + 1) * 10]; if (photoBtn) { UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:(CGSizeMake(60, 40))]; [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } else { [tableView beginUpdates]; [cell(Layout setFrame:cell(Layout).frame); [tableView endUpdates]; } } } } }

4. 设置单元格内容

在cellForRowAtIndexPath中设置单元格内容

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"];  if (!cell) {      cell = [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"];  }  CustomPhotoBtn *photoBtn = [CustomPhotoBtn customPhotoBtn];  photoBtn.fileFullName.textColor = [UIColor blueColor];  NSString *imageName = [url stringByAppendingString:@".temp"];  NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[      @"Library/Caches/"      stringByAppendingString:imageName  ]];  NSData *imageData = [NSData dataWithContentsOfFile:imageDataPath];  if (imageData) {      UIImage *image = [UIImage imageWithData:imageData];      if (image) {          UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))];          if (thumbImg) {              [photoBtn.imgView setImage:thumbImg];              [cell.contentView addSubview:photoBtn];          } else {              [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]];          }      } else {          [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]];      }  } else {      [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]];  }[cell.contentView addSubview:photoBtn];  return cell;

5. 最后运行代码

确保项目中正确引入必要的头文件,并确保所有必要的控件已经在xib文件中定义。运行项目测试表格是否正确加载图片,如果图片缓存存在,直接显示;否则按需求加载和缓存。

通过这些步骤,您可以轻松实现带有UIScrollView和UITableView的缓存图片加载功能,同时确保性能优化和用户体验良好。

上一篇:file not found app文件
下一篇:NSNotification、delegate和KVO的区别

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月16日 12时12分22秒