多线程-NSThread
点击工程中“info.plist”,
添加“App Transport Security Settings”,
“Allow Arbitrary Loads--YES”
#import „ViewController.h"
@interface ViewController ()
{
NSMutableArray *_imageViewArray;
}
@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];
调用方法
[self layout];}
-(void)layout
{
------------------不用线程--------------------
_imageView=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_imageView.contentMode=UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageView];
----------------九宫格(多线程)------------------
_imageViewArray=[NSMutableArray array];
创建九宫格
for (int row=0; row<3; row++)
{
for (int colume=0; colume<3; colume++)
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+colume*118.75, 20+row*118.75, 100, 100)];
imageView.backgroundColor=[UIColor magentaColor];
[self.view addSubview:imageView ];
[_imageViewArray addObject:imageView];}}
控件
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 500, 275, 30)];
[button setTitle:@"加载图片" forState:UIControlStateNormal];
[button setTitle:@"加载完成" forState:UIControlStateSelected];
[button addTarget:self action:@selector(loadImageWithThread:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor cyanColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[self.view addSubview:button];}
-----------------不用线程--------------------
#pragma mark 不用线程
-(void)loadImage
{UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@„http://b.hiphotos.baidu.com/zhidao/pic/item/0823dd54564e9258706bb38b9f82d158ccbf4e73.jpg"]]];
UIImage *image=[UIImage imageNamed:@"屏幕快照 2016-09-03 下午3.43.08"];
_imageView.image=image;}
-------------------使用线程---------------------
#pragma mark 使用线程
-(void)loadImageWithThread:(UIButton *)sender
{
按钮的是否选择
sender.selected=!sender.selected;
------------------创建单线程---------------------
方法1:
使用对象方法创建线程
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(myThread) object:nil];
执行线程(启动一个线程,并不立即执行,处于就绪状态,当系统调度时才执行)
[thread start];
方法2:
使用类方法创建线程
[NSThread detachNewThreadSelector:@selector(myThread) toTarget:self withObject:nil];
------------------创建多线程---------------------
优先级
NSThread的线程的优先级,只是提高优先被加载的几率
制约: 网络状况
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(myThread2:) object:[NSNumber numberWithInt:8]];
thread2.threadPriority=1;//范围0-1
[thread2 start];
创建多线程
for (int i=0; i<9; i++)
{
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(myThread2:) object:[NSNumber numberWithInt:i]];
thread2.name=[NSString stringWithFormat:@"线程----:%d",i];
[thread2 start];
thread2.threadPriority=0;
}}
------------------单线程(子线程)---------------
-(void)myThread
{
NSLog(@"已进入线程");
耗时操作(放入子线程),子线程不加载UI
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@„http://b.hiphotos.baidu.com/zhidao/pic/item/0823dd54564e9258706bb38b9f82d158ccbf4e73.jpg"]];
NSObject的分类方法,每个NSObject对象都有这个方法
[self performSelectorOnMainThread:@selector(mainThreadHandle:) withObject:data waitUntilDone:YES];}
----------------单线程回归主线程------------------
#pragma mark 回归主线程的一个更新UI的方法
-(void)mainThreadHandle:(NSData *)data
{
_imageView.image=[UIImage imageWithData:data];
}
---------------多线程(子线程)-----------------
#pragma mark 子线程(带参数)
-(void)myThread2:(NSNumber *)index
{
NSLog(@"已进入线程222");
NSLog(@"线程----%@",[NSThread currentThread].name);
if (![index isEqual:@8])
{
线程休眠时间
[NSThread sleepForTimeInterval:3];}
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://pic2.desk.chinaz.com/file/09.02.10/8/fengus5%@.jpg",index]]];
NSArray *array=@[data,index];
NSObject的分类方法,每个NSObject对象都有这个方法
[self performSelectorOnMainThread:@selector(upDateImage:) withObject:array waitUntilDone:YES];}
----------------多线程回归主线程------------------
#pragma mark 回归主线程的一个更新UI的方法
-(void)upDateImage:(NSArray *)arr
{
UIImage *image=[UIImage imageWithData:arr[0]];
UIImageView *imageView=_imageViewArray[[arr[1] intValue]];
imageView.image=image;
}
@end
多线程-NSOperation
点击工程中“info.plist”,
添加“App Transport Security Settings”,
“Allow Arbitrary Loads--YES”
#import „ViewController.h"
宏定义
#define ROW 3
#define COLUMN 5
#define IMAGE_COUNT ROW*COLUMN
#define WIDTH 100
#define HEIGHT WIDTH
@interface ViewController ()
{
NSMutableArray *_imageViewArray;
NSMutableArray *_array;
}
@end
@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];
_imageViewArray=[NSMutableArray array];
_array=[NSMutableArray array];
for (int i=0; i<ROW; i++)
{
for (int j=0; j<COLUMN; j++)
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+118.75*i, 20+118.75*j, WIDTH, HEIGHT)];
[self.view addSubview:imageView];
[_imageViewArray addObject:imageView];
}
}
[self begin];
}
#pragma mark 开启多线程
-(void)begin
{
创建一个操作队列
NSOperationQueue *operationQueue=[[NSOperationQueue alloc]init];
设置最大并发线程数
operationQueue.maxConcurrentOperationCount=1;
向队列添加操作
for (int i=0; i<IMAGE_COUNT; i++)
{
方法1
创建操作块,添加到队列
NSBlockOperation *blockOperation=[NSBlockOperation blockOperationWithBlock:^{
子线程
[self loadImage:[NSNumber numberWithInt:i]];}];
添加到队列
[operationQueue addOperation:blockOperation];
方法2
祷文invocation
NSInvocationOperation *InvocationOp=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
添加到队列
[operationQueue addOperation:InvocationOp];}
方法3
控制线程的执行顺序
一个串行队列
for (int i=0; i<IMAGE_COUNT; i++)
{
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{
[self loadImage:[NSNumber numberWithInt:i]];}];
设置依赖(op2依赖op1)
[op2 addDependency:op];
[operationQueue addOperation:op];
-----------------------1------------------------
设置依赖
for (int i=0; i<IMAGE_COUNT-1; i++)
{
[_array[i+1] addDependency:_array[i]];
[operationQueue addOperation:_array[i]];
}
添加到队列
[operationQueue addOperation:_array[14]];
-----------------------2------------------------
设置依赖
for (int i=0; i<IMAGE_COUNT-1; i++)
{
[_array[i+1] addDependency:_array[i]];
}
添加到队列
for (int i=0; i<IMAGE_COUNT; i++)
{
[operationQueue addOperation:_array[i]];
}}
------------------方法子线程---------------------
#pragma mark 方法1子线程
-(void)loadImage:(NSNumber *)index
{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://pic2.desk.chinaz.com/file/09.02.10/8/fengus%d.jpg",[index intValue]+1]]];
回归主线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
主线程
UIImage *image=[UIImage imageWithData:data];
UIImageView *imageView=_imageViewArray[[index intValue]];
imageView.image=image;}];}
@end