企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
前两篇是自己写的下载图片方法,现在用第三方框架只要几行代码就可以实现图片的下载。SDWebImage底层实现的思路也是和前面说的一样。 SDWebImage是网络图片处理框架,封装很很多方法,例如:图片下载、图片缓存、下载进度监听、gif处理等等。大大提高了网络图片处理的效率。值得使用![奋斗](https://box.kancloud.cn/2016-02-17_56c446a99dec4.gif) 。 github托管地址:[https://github.com/rs/SDWebImage](https://github.com/rs/SDWebImage) 实现cell图片下载之前,先了解一下SDWebImage的使用: 1、导入框架,引入头文件: ~~~ #import "UIImageView+WebCache.h" ~~~ 2、图片下载并缓存方法: UIImageView+WebCache.h ~~~ //得到当前图片的url - (NSURL *)sd_imageURL; /** * 异步下载图片并缓存 */ - (void)sd_setImageWithURL:(NSURL *)url; /** * 异步下载图片并缓存,没下载完之前先显示占位图片,下载完之后再替换 */ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; /** * 异步下载图片并缓存 * @param url 下载图片路径 * @param placeholder 占位图片,直到下载完成才替换 * @param options 下载图片选择方式 */ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; /** * 异步下载图片并缓存,完成后可以在block中做事情 * @param url 下载图片url * @param completedBlock SDWebImageCompletionBlock:UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) block中可以得到下载图片,错误信息,缓存类型,下载图片地址 参数,给用户做相应操作 */ - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock; /** * 异步下载图片并缓存,提供占位图片,并完成后可以在block中做事情 * * @param url T下载图片url * @param placeholder T占位图片,直到下载完成才替换 * @param completedBlock SDWebImageCompletionBlock:UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) block中可以得到下载图片,错误信息,缓存类型,下载图片地址 参数,给用户做相应操作 */ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock; /** * 异步下载图片并缓存,完成后可以在block中做事情 * @param url 下载图片路径 * @param placeholder 占位图片,直到下载完成才替换 * @param options 下载图片选择方式 * @param completedBlock 同上 */ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; /** * 异步下载图片并缓存,可以监听下载进度,完成后可以在block中做事情 * @param url 下载图片路径 * @param placeholder 占位图片,直到下载完成才替换 * @param options 下载图片选择方式 * @param progress * SDWebImageDownloaderProgressBlock:NSInteger receivedSize(当前下载大小), NSInteger expectedSize(总大小) * @param completedBlock 同上 */ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock; /** * 异步下载图片并缓存,可以监听下载进度,完成后可以在block中做事情 * @param url 下载图片路径 * @param placeholder 占位图片,直到下载完成才替换 * @param options 下载图片选择方式 * @param progress * SDWebImageDownloaderProgressBlock:NSInteger receivedSize(当前下载大小), NSInteger expectedSize(总大小) * @param completedBlock 同上 */ - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock; ~~~ 3、options所有选项 ~~~ //失败后重新下载 SDWebImageRetryFailed = 1 << 0, //最低优先级,当正在进行UI交互时,自动暂停内部的一些下载操作 SDWebImageLowPriority = 1 << 1, //只缓存内存 SDWebImageCacheMemoryOnly = 1 << 2, //渐进式下载,显示的图像是逐步在下载 SDWebImageProgressiveDownload = 1 << 3, //刷新缓存 SDWebImageRefreshCached = 1 << 4, //后台下载 SDWebImageContinueInBackground = 1 << 5, /** * Handles cookies stored in NSHTTPCookieStore by setting * NSMutableURLRequest.HTTPShouldHandleCookies = YES; */ SDWebImageHandleCookies = 1 << 6, //允许使用无效的SSL证书 SDWebImageAllowInvalidSSLCertificates = 1 << 7, //高优先级下载 SDWebImageHighPriority = 1 << 8, //延迟占位符 SDWebImageDelayPlaceholder = 1 << 9, //改变动画形象 SDWebImageTransformAnimatedImage = 1 << 10, /** * By default, image is added to the imageView after download. But in some cases, we want to * have the hand before setting the image (apply a filter or add it with cross-fade animation for instance) * Use this flag if you want to manually set the image in the completion when success */ SDWebImageAvoidAutoSetImage = 1 << 11 ~~~ 4、内存处理:当app接收到内存警告时,我们要释放内存。 ~~~ SDWebImageManager *manager = [SDWebImageManager sharedManager]; // 取消正在下载的操作 [manager cancelAll]; // 清除内存缓存 [manager.imageCache clearMemory]; //释放磁盘的缓存 [manager.imageCache cleanDisk]; ~~~ 上面只是实现图片下载的方法和使用步骤而已,SDWebImage还要很多强大的功能,如本地缓存引入SDImageCache.h,SDImageCache文件里面可以看到它提供了很多方法,并可以设置缓存的时间(默认一个星期),缓存大小等等。具体用到我们看一下相应的头文件提高的方法即可,这里不一一详述。 cell下载图片(SDWebImage)  demo: ~~~ // // AppsViewController.m // cell图片下载-SDWebImage // #import "AppsViewController.h" #import "App.h" #import "UIImageView+WebCache.h" @interface AppsViewController () @property(nonatomic,strong) NSArray *apps; @end @implementation AppsViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSArray *)apps{ if (!_apps) { NSMutableArray *appArr = [NSMutableArray array]; NSString *file = [[NSBundle mainBundle]pathForResource:@"apps" ofType:@"plist"]; NSArray *dictArr = [NSArray arrayWithContentsOfFile:file]; for (NSDictionary *dict in dictArr) { App *app = [App appWithDict:dict]; [appArr addObject:app]; } _apps = appArr; } return _apps; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.apps.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"app"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } App *app = self.apps[indexPath.row]; cell.textLabel.text = app.name; cell.detailTextLabel.text = app.download; //下载图片 NSURL *url = [NSURL URLWithString:app.icon]; //[cell.imageView sd_setImageWithURL:url]; //[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]]; //[cell.imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { // NSLog(@"%@",imageURL); //}]; //SDWebImage下载图片 SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority; [cell.imageView sd_setImageWithPreviousCachedImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"] options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"下载进度:%f",(double)receivedSize/expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSLog(@"下载图片完成%@",image); }]; return cell; } @end ~~~ 注意:app接收到内存警告时,要释放内存。 整个程序的要实现警告释放内存,因此在AppDelegate.m 的applicationDidReceiveMemoryWarning方法中释放。 ~~~ -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{ SDWebImageManager *manager = [SDWebImageManager sharedManager]; // 1.取消正在下载的操作 [manager cancelAll]; // 2.清除内存缓存 [manager.imageCache clearMemory]; } ~~~