图片的加载

利用多线程来加载图片,下面是具体代码:

在上一篇创建的工程上面继续添加代码,新建一个类YueThirdViewController和一个plist,plist中有一个数组,数组中4个string类型的元素,存放的是图片的网络地址。

再创建一个继承UITableView的类YueCell。

先看YueCell.h和YueCell.m中的代码


#import "YueCell.h"

@implementation YueCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //初始化
        self.pActivityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        //设置显示区域
        self.pActivityView.frame = CGRectMake(10, 10, 80, 80);
        //加到当前视图当中
        [self addSubview:self.pActivityView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

}

- (void)dealloc
{
    [_pActivityView release];
    [super dealloc];
}
@end
下面是YueThirdViewController.h的代码

YueThirdViewController.m中的代码:

#import "YueThirdViewController.h"

#import "YueCell.h"

@interface YueThirdViewController ()

@end

@implementation YueThirdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"Thread-Three";
    }
    return self;
}

- (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];
    
    //获取文件路径
    NSString *pPath = [[NSBundle mainBundle]pathForResource:@"myData" ofType:@"plist"];
    //初始化路径
    self.mArr = [[NSArray alloc]initWithContentsOfFile:pPath];
    //初始化线程池
    self.pQueue = [[NSOperationQueue alloc]init];
    
}

#pragma mark----------tableview dataSource--------
//每段有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.mArr count];
}
//每行的渲染
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    //重用cell
    YueCell *pCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //如果为空,就创建出来
    if (pCell == nil)
    {
        pCell = [[YueCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    //获取行数
    //NSUInteger row = [indexPath row];
    //在数组中找到对应下标的值
    //pCell.textLabel.text = [self.mArr objectAtIndex:row];
    [pCell.pActivityView startAnimating];
    //创建子线程
    NSInvocationOperation *pInvocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage:) object:indexPath];
    //将线程放入线程池由线程池管理
    [self.pQueue addOperation:pInvocation];
    return pCell;
}

- (void)downLoadImage:(NSIndexPath *)path
{
    NSInteger row = [path row];
    NSString *pStr = [self.mArr objectAtIndex:row];
    //转换为URL地址
    NSURL *url = [NSURL URLWithString:pStr];
    //将网络资源转换为NSData
    NSData *pDara = [NSData dataWithContentsOfURL:url];
    //获取图片资源
    UIImage *pImage = [UIImage imageWithData:pDara];
    //找到当前的行
    YueCell *pCell = (YueCell *)[self.mTableView cellForRowAtIndexPath:path];
    //设置cell里的图片
    pCell.imageView.image = pImage;
    
    //停止
    [pCell.pActivityView stopAnimating];
    //移除
    [pCell.pActivityView removeFromSuperview];
}

#pragma mark-----------tableview delegate-----------------
//每行的高度
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_mTableView release];
    [_mArr release];
    [_pQueue release];
    [super dealloc];
}
运行,看一下运行效果:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值