iOS学习笔记——网络资源下载(下载图片)

来源:互联网 发布:返利能看到淘宝订单吗 编辑:程序博客网 时间:2024/06/11 19:01

根据获取的URL地址,通过解析处理,在连接网络的情况下,下载图片。

首先创建一个包含图片URL的.plist文件,如图所示:


创建一个LinCell类,继承于UITableViewCell,用于绘制表视图的行信息。此处是创建活动标志,记录图片是否下载完毕。

 在.h文件里代码:

@interface LinCell : UITableViewCell//创建活动标志对象@property (retain, nonatomic) UIActivityIndicatorView * pActivityView;@end
在.m文件里代码:

@implementation LinCell//释放创建的对象- (void)dealloc{    [_pActivityView release];    [super dealloc];}- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        //初始化活动标志        self.pActivityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];        //设置活动标志的范围        [self.pActivityView setFrame:CGRectMake(10, 10, 40, 40)];        //把活动标志添加到当前视图        [self addSubview:self.pActivityView];            }    return self;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];}@end
在视图控制器中,需要遵循UITableViewDataSource协议和UITableViewDelegate协议。下载的过程把每个下载事件放入线程池中,由系统管理。

在.h文件中代码:

@interface LinViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>//创建表视图对象@property (retain, nonatomic) UITableView * mTableView;//创建数组对象@property (retain, nonatomic) NSArray * mArray;//创建线程池对象@property (retain, nonatomic) NSOperationQueue * mQueue;@end
在.m文件里的代码:(需要前导入LinCell类)

@implementation LinViewController//释放创建的对象- (void)dealloc{    [_mTableView release];    [_mArray release];    [_mQueue release];    [super dealloc];}- (void)viewDidLoad{    [super viewDidLoad];    //初始化表视图    self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];    //设置表视图的委托对象    self.mTableView.dataSource = self;    self.mTableView.delegate = self;    //把表视图添加到当前视图中    [self.view addSubview:self.mTableView];        //获取.plist文件的路径    NSString * pPath = [[NSBundle mainBundle]pathForResource:@"myData" ofType:@"plist"];    //初始化数组,存储.plist文件的内容    self.mArray = [[NSArray alloc]initWithContentsOfFile:pPath];        //初始化线程池    self.mQueue = [[NSOperationQueue alloc]init];}#pragma mark---UITableViewDataSource//获取表视图的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.mArray count];}//渲染表视图的每一行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //设置静态字符串,作为标记    static NSString * identifer = @"identifer";    //获取缓存中的cell内容    LinCell * pCell = (LinCell *)[tableView dequeueReusableCellWithIdentifier:identifer];    //判断如果为空则创建新的cell    if (nil == pCell)    {        pCell = [[LinCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];    }    //设置活动标志开始运行,表示进度开始    [pCell.pActivityView startAnimating];    //创建子线程    NSInvocationOperation * pInvocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage:) object:indexPath];    //将线程放入线程池,由线程池去管理    [self.mQueue addOperation:pInvocation];        return pCell;}//下载图片的方法,必须在网络连接的情况下- (void)downLoadImage:(NSIndexPath *)path{    //获取表视图行数    NSInteger row = [path row];    //获取数组中相应为止的元素    NSString * pString = [self.mArray objectAtIndex:row];    //转化为RUL地址    NSURL * pURL = [NSURL URLWithString:pString];    //将网络资源转化为NSData类型    NSData * pData = [[NSData alloc]initWithContentsOfURL:pURL];    //获取网络图片    UIImage * pImage = [UIImage imageWithData:pData];    //找到当前行    LinCell * pCell = (LinCell *)[self.mTableView cellForRowAtIndexPath:path];    //设置cell里的图片    pCell.imageView.image = pImage;    //设置活动标志停止    [pCell.pActivityView stopAnimating];    //移除活动标志    [pCell.pActivityView removeFromSuperview];}#pragma mark-------UITableViewDelegate//获取表视图中每一行的高度- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 60;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@end




0 0
原创粉丝点击