轻量级UIImageView分类缓存 库 AsyncImageView 使用
轻量级UIImageView分类缓存 库 AsyncImageView 使用
一:
- AsyncImageView 主页:https://github.com/nicklockwood/AsyncImageView
- 只包含了一个 .h 一个 .m文件
- 兼容 iOS 5.0及以上,以及ARC
二:使用
主要演示结合UITableview的使用
demo代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //create new cell cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //common settings cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; cell.imageView.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f); cell.imageView.clipsToBounds = YES; } else { //cancel loading previous image for cell [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView]; } //set placeholder image or cell won't update when image is loaded cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; //load the image cell.imageView.imageURL = self.imageURLs[(NSUInteger)indexPath.row]; //display image path cell.textLabel.text = [[(NSURL *)self.imageURLs[(NSUInteger)indexPath.row] path] lastPathComponent]; return cell; }
demo 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; #define IMAGE_VIEW_TAG 99 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //create new cell cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //add AsyncImageView to cell AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; imageView.tag = IMAGE_VIEW_TAG; [cell addSubview:imageView]; //common settings cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; cell.indentationWidth = 44.0f; cell.indentationLevel = 1; } //get image view AsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG]; //cancel loading previous image for cell [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView]; //load the image imageView.imageURL = [_imageURLs objectAtIndex:indexPath.row]; //display image path cell.textLabel.text = [[[_imageURLs objectAtIndex:indexPath.row] path] lastPathComponent]; return cell; }
三:可用api

@interface AsyncImageLoader : NSObject + (AsyncImageLoader *)sharedLoader; + (NSCache *)defaultCache; @property (nonatomic, strong) NSCache *cache; @property (nonatomic, assign) NSUInteger concurrentLoads; @property (nonatomic, assign) NSTimeInterval loadingTimeout; - (void)loadImageWithURL:(NSURL *)URL target:(id)target success:(SEL)success failure:(SEL)failure; - (void)loadImageWithURL:(NSURL *)URL target:(id)target action:(SEL)action; - (void)loadImageWithURL:(NSURL *)URL; - (void)cancelLoadingURL:(NSURL *)URL target:(id)target action:(SEL)action; - (void)cancelLoadingURL:(NSURL *)URL target:(id)target; - (void)cancelLoadingURL:(NSURL *)URL; - (void)cancelLoadingImagesForTarget:(id)target action:(SEL)action; - (void)cancelLoadingImagesForTarget:(id)target; - (NSURL *)URLForTarget:(id)target action:(SEL)action; - (NSURL *)URLForTarget:(id)target; @end @interface UIImageView(AsyncImageView) @property (nonatomic, strong) NSURL *imageURL; @end @interface AsyncImageView : UIImageView @property (nonatomic, assign) BOOL showActivityIndicator; @property (nonatomic, assign) UIActivityIndicatorViewStyle activityIndicatorStyle; @property (nonatomic, assign) NSTimeInterval crossfadeDuration; @end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具