网络多线程 ---实现网络负载图片
案例要求:网络负载图片,随机装上树立了良好的视图
实现的效果图例如以下:
详细代码例如以下:
//
// ViewController.m
// 0603---NSOperationQueue
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray * imageArray;
@end
@implementation ViewController
#pragma mark - 懒载入
- (NSMutableArray *)imageArray
{
if (_imageArray==nil)
{
_imageArray=[NSMutableArray array];
}
return _imageArray;
}
#pragma mark - 入口
- (void)viewDidLoad {
[super viewDidLoad];
//载入视图
[self _loadViews];
//开启多线程载入图片
[self _loadThread];
}
- (void)_loadViews
{
for (int i=0; i<15; i++)
{
int col=i%3;
int row=i/3;
UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(22+col*(80+45), 40+row*(100+20), 90, 90)];
imageView.backgroundColor=[UIColor redColor];
[self.imageArray addObject:imageView];
[self.view addSubview:imageView];
}
}
- (void)_loadThread
{
//开启多线程
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
for(int i=0;i<self.imageArray.count;i++)
{
NSBlockOperation * block=[NSBlockOperation blockOperationWithBlock:^{
//下载图片
NSString * imageSrc=@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%d.jpg";
imageSrc=[NSString stringWithFormat:imageSrc,i]; //拼接字符串
NSURL * URL = [NSURL URLWithString:imageSrc];
NSData * data = [NSData dataWithContentsOfURL:URL];
UIImage * image=[UIImage imageWithData:data];
//获取主线程,刷新UI
NSOperationQueue * queue2=[NSOperationQueue mainQueue];
[queue2 addOperationWithBlock:^{
//刷新图片,给imageView赋值
UIImageView * imageView=self.imageArray[i];
imageView.image=image;
}];
}];
[queue addOperation:block];
}
}
@end
0602---小结:
多线程的三种创建方式
NSThread
1.实例方法:(调用start方法开启多线程)
[NSThread alloc]initWithTarget:nil selector:nil withObject:nil];
2.类方法
[NSThread detachNewThreadSelector:nil toTarget:nil withObject:nil];
3.刷新UI必须通过主线程
[self performSelectorOnMainThread:nil withObject:nil waitUntilDone:nil];
(*)4.隐式开启多线程
[self performSelectorInBackground:nil withObject:nil];
0603- - - -NSOperationQueue
1.有对象方法,没有类方法
创建多线程(4种方式):
// 1> 採用NSOperation的子类NSInvocationOperation来开启多线程(必须加入到队列)
NSInvocationOperation * nvo=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(show) object:nil];
[queue addOperation:nvo];
// 2> 採用NSBlockOperation 来开启多线程(必须加入到队列)
NSBlockOperation * block=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"我是block");
}];
[queue addOperation:block];
// 3> 採用队列(不须又一次加入到队列)
[queue addOperationWithBlock:^{
NSLog(@"我是queue创建的block");
}];
// 4>
NSBlockOperation * block=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation");
}];
[block addExecutionBlock:^{
NSLog(@"NSBlockOperation===1");
}];
[block addExecutionBlock:^{
NSLog(@"NSBlockOperation===2");
}];
[block addExecutionBlock:^{
NSLog(@"NSBlockOperation===3");
}];
[block start];
2.指定线程的运行顺序- - - 依赖(addDependency)
//创建block1
NSBlockOperation * block1=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"我是block1");
}];
[queue addOperation:block1]; //加入到队列
//创建block2
NSBlockOperation * block2=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"我是block2");
}];
[block2 addDependency:block1]; //依赖---先后运行顺序(block1 > block2)
[queue addOperation:block2]; //加入到队列
3.刷新UI
//开启多线程
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
NSBlockOperation * block=[NSBlockOperation blockOperationWithBlock:^{
//下载图片
{
}
//获取主线程。刷新UI
NSOperationQueue * queue2=[NSOperationQueue mainQueue];
[queue2 addOperationWithBlock:^{
//刷新图片,给imageView赋值
{
}
}];
}];
[queue addOperation:block];
版权声明:本文博主原创文章,博客,未经同意不得转载。