案例要求:网络加载图片,随机加载到设置好的视图上
实现的效果图如下:
具体代码如下:
//
// 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];