轻量级UIImageView分类缓存 库 AsyncImageView 使用
生活随笔
收集整理的這篇文章主要介紹了
轻量级UIImageView分类缓存 库 AsyncImageView 使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
輕量級UIImageView分類緩存 庫 AsyncImageView 使用
一:
- AsyncImageView 主頁:https://github.com/nicklockwood/AsyncImageView
- 只包含了一個 .h 一個 .m文件
- 兼容 iOS 5.0及以上,以及ARC
二:使用
? ?主要演示結(jié)合UITableview的使用
? ? demo代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil){//create new cellcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];//common settingscell.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 loadedcell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];//load the imagecell.imageView.imageURL = self.imageURLs[(NSUInteger)indexPath.row];//display image pathcell.textLabel.text = [[(NSURL *)self.imageURLs[(NSUInteger)indexPath.row] path] lastPathComponent];return cell; } View Code?
? ? ?demo 代碼:
??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";#define IMAGE_VIEW_TAG 99UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil){//create new cellcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];//add AsyncImageView to cellAsyncImageView *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 settingscell.selectionStyle = UITableViewCellSelectionStyleNone;cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;cell.indentationWidth = 44.0f;cell.indentationLevel = 1;}//get image viewAsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];//cancel loading previous image for cell [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];//load the imageimageView.imageURL = [_imageURLs objectAtIndex:indexPath.row];//display image pathcell.textLabel.text = [[[_imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];return cell; } View Code?
三:可用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 View Code?
?
總結(jié)
以上是生活随笔為你收集整理的轻量级UIImageView分类缓存 库 AsyncImageView 使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【英语】英语学习之道
- 下一篇: 利用System.Net.Mail和多线