/**
NSThread
* 创建手动开启方式
* 第三个参数 就是方法选择器选择方法的参数
*/
/* NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread1:) object:@"threadooo"];
// 开启线程
[thread start];
*/
/**
* 创建并自动开启的方式
*
*
*/
[ NSThread detachNewThreadSelector : @selector (thread1:) toTarget : self withObject : @"threadooo" ];
NSLog ( @"viewDidLoad 方法所在的线程 %@" ,[ NSThread currentThread ]);
NSThread
* 创建手动开启方式
* 第三个参数 就是方法选择器选择方法的参数
*/
/* NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread1:) object:@"threadooo"];
// 开启线程
[thread start];
*/
/**
* 创建并自动开启的方式
*
*
*/
[ NSThread detachNewThreadSelector : @selector (thread1:) toTarget : self withObject : @"threadooo" ];
NSLog ( @"viewDidLoad 方法所在的线程 %@" ,[ NSThread currentThread ]);
加载网络图片的格式:
一.加载一张图片:
加载一张图片的过程
:
1.
创建一个
UIImageView
,并放在父视图上
2.
创建一个子线程
3. 通过 URL 获取网络图片
4. 回到主线程
3. 通过 URL 获取网络图片
4. 回到主线程
5.在主线程更新UI
二.加载多张图片的过程:
2.创建多个
UIImageView
,并放在父视图上
int imageIndex = 100;
for (int row = 0; row<3; row++) {
for (int list = 0; list<2; list++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30+list*200, 30+row*200, 150, 150)];
imageView.backgroundColor = [UIColor cyanColor];
imageView.tag =imageIndex++;
for (int row = 0; row<3; row++) {
for (int list = 0; list<2; list++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30+list*200, 30+row*200, 150, 150)];
imageView.backgroundColor = [UIColor cyanColor];
imageView.tag =imageIndex++;
[self.view addSubview:imageView];
}
}
3.创建多个子线程:
for (int index = 0; index <6; index++) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:@(index)];
[thread start];//开启线程
//把每次要出现的子线程加进一个数组内
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:@(index)];
[thread start];//开启线程
//把每次要出现的子线程加进一个数组内
[threadArray addObject:thread];
4.加载网络图片:
-(void)thread:(NSNumber *)index{
//把每次要出现的子线程加进一个数组内
// [threadArray addObject:[NSThread currentThread]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
image = [UIImage imageWithData:data];
//休眠加载 一个一个的延迟加载
[NSThread sleepForTimeInterval:[index intValue]];
NSThread *thread =[NSThread currentThread];
if (thread.isCancelled == YES) {
[NSThread exit];//停止线程
}
//回到主线程
//把每次要出现的子线程加进一个数组内
// [threadArray addObject:[NSThread currentThread]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kUrl]];
image = [UIImage imageWithData:data];
//休眠加载 一个一个的延迟加载
[NSThread sleepForTimeInterval:[index intValue]];
NSThread *thread =[NSThread currentThread];
if (thread.isCancelled == YES) {
[NSThread exit];//停止线程
}
//回到主线程
[
self performSelectorOnMainThread:@selector(updateUI:) withObject:index waitUntilDone:YES
];
}
5.更新UI
-(void)updateUI:(NSNumber *)index{
//
UIImageView *imageView = [self.view viewWithTag:[index intValue]+100];
imageView.image = image;
//
UIImageView *imageView = [self.view viewWithTag:[index intValue]+100];
imageView.image = image;
}
取消子线程的方法
首先要在创建子线程的地方用一个全局变量的数组去接收创建的子线程(注意:在创建子线程的时候一定要用手动方式创建
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:@(index)];
[thread start];//开启线程
[thread start];//开启线程
//把每次要出现的子线程加进一个数组内
[threadArray addObject:thread];)其次就是在加载图片的那个方法里面添加一个if语句判断子线程的状态(方法:
NSThread *thread =[NSThread currentThread];
if (thread.isCancelled == YES) {
[NSThread exit];//停止线程
if (thread.isCancelled == YES) {
[NSThread exit];//停止线程
}
)然后使用系统的方法取消未完成的子线程的方法方法如下
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
for (int index = 0 ; index<6; index++) {
NSThread *thread = threadArray[index];
//点击屏幕 取消未完成的子线程
if (thread.isFinished == NO) {
[thread cancel];//取消线程
}
}
NSLog(@"所有子线程%@",threadArray);
for (int index = 0 ; index<6; index++) {
NSThread *thread = threadArray[index];
//点击屏幕 取消未完成的子线程
if (thread.isFinished == NO) {
[thread cancel];//取消线程
}
}
NSLog(@"所有子线程%@",threadArray);
}