多线程状态下加载网络图片,以及取消子线程加载图片,开启线程的两种方式

/**
     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.
回到主线程
 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++;
            [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];//开启线程
       
//把每次要出现的子线程加进一个数组内
        [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];//停止线程
    }

   
   
//回到主线程
    [ 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;
   
}
取消子线程的方法
首先要在创建子线程的地方用一个全局变量的数组去接收创建的子线程(注意:在创建子线程的时候一定要用手动方式创建 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:@(index)];
         [thread
start];//开启线程
        //把每次要出现的子线程加进一个数组内
        [threadArray addObject:thread];)其次就是在加载图片的那个方法里面添加一个if语句判断子线程的状态(方法: NSThread *thread =[NSThread currentThread];
   
   
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);
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值