-(UIImage *) getImageFromURL:(NSString *)fileURL {
//NSLog(@"执行图片下载函数");
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
#import "XNViewController.h"
#import "XNApp.h"
@interface XNViewController ()
@property (nonatomic, strong) NSArray *appList;
@property (nonatomic, strong) NSOperationQueue *queue;
@end
@implementation XNViewController
#pragma mark - 懒加载
- (NSOperationQueue *)queue {
if (!_queue) _queue = [[NSOperationQueue alloc] init];
return _queue;
}
//可抽取出来写到模型中
- (NSArray *)appList {
if (!_appList) {
//1.加载plist到数组中
NSURL *url = [[NSBundle mainBundle] URLForResource:@"apps.plist" withExtension:nil];
NSArray *array = [NSArray arrayWithContentsOfURL:url];
//2.遍历数组
NSMutableArray *arrayM = [NSMutableArray array];
[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
[arrayM addObject:[XNApp appWithDict:obj]]; //数组中存放的是字典, 转换为app对象后再添加到数组
}];
_appList = [arrayM copy];
}
return _appList;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 88;
// NSLog(@"appList-%@",_appList);
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//用模型来填充每个cell
XNApp *app = self.appList[indexPath.row];
cell.textLabel.text = app.name; //设置文字
//设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
if (!app.image) {
cell.imageView.image = [UIImage imageNamed:@"user_default"];
[self downloadImg:indexPath];
}
else {
//直接用模型中的内存缓存
cell.imageView.image = app.image;
}
// NSLog(@"cell--%p", cell);
return cell;
}
/**始终记住, 通过模型来修改显示. 而不要试图直接修改显示*/
- (void)downloadImg:(NSIndexPath *)indexPath {
XNApp *app = self.appList[indexPath.row]; //取得改行对应的模型
[self.queue addOperationWithBlock: ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到图像数据
UIImage *image = [UIImage imageWithData:imgData];
//在主线程中更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
//通过修改模型, 来修改数据
app.image = image;
//刷新指定表格行
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}];
}
@end
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//用模型来填充每个cell
XNApp *app = self.appList[indexPath.row];
cell.textLabel.text = app.name; //设置文字
// //设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
// if (!cell.imageView.image) {
// cell.imageView.image = [UIImage imageNamed:@"user_default"];
//
// [self downloadImg:indexPath];
// }
// else {
// //直接用模型中的内存缓存
// cell.imageView.image = app.image;
// }
//使用SDWebImage来完成上面的功能. 针对ImageView.
//一句话, 自动实现了异步下载. 图片本地缓存. 网络下载. 自动设置占位符.
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"user_default"]];
return cell;
}
/**始终记住, 通过模型来修改显示. 而不要试图直接修改显示*/
//- (void)downloadImg:(NSIndexPath *)indexPath {
// XNApp *app = self.appList[indexPath.row]; //取得改行对应的模型
//
// [self.queue addOperationWithBlock: ^{
// NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到图像数据
// UIImage *image = [UIImage imageWithData:imgData];
//
// //在主线程中更新UI
// [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
// //通过修改模型, 来修改数据
// app.image = image;
// //刷新指定表格行
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
// }];
// }];
//}
@end
*SDWebImageRetryFailed = 1<< 0, 默认选项,失败后重试 *SDWebImageLowPriority = 1<< 1, 使用低优先级 *SDWebImageCacheMemoryOnly = 1<< 2, 仅仅使用内存缓存 *SDWebImageProgressiveDownload = 1<< 3, 显示现在进度 *SDWebImageRefreshCached = 1<< 4, 刷新缓存 *SDWebImageContinueInBackground =1 << 5, 后台继续下载图像 *SDWebImageHandleCookies = 1<< 6, 处理Cookie *SDWebImageAllowInvalidSSLCertificates= 1 << 7, 允许无效的SSL验证 *SDWebImageHighPriority = 1<< 8, 高优先级 *SDWebImageDelayPlaceholder = 1<< 9 延迟显示占位图片
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有