NSThread中cancle与exit的使用

来源:互联网 发布:德安东尼 知乎 编辑:程序博客网 时间:2024/06/06 07:08

@interface ViewController ()


/** 图片视图*/

@property(nonatomic,weak)UIImageView * imageView;


/** 图片数组*/

@property(nonatomic,strong)NSMutableArray * imageArray;


/** 存储线程*/

@property(nonatomic,strong)NSMutableArray * threadArray;


@end


//对象方法cancle,可以在外部使用。

//类方法exit,写在线程内部,结束当前现线程。

//两者结合使用,使用cancle进行标记,使用exit退出


@implementation ViewController

- (NSMutableArray *)imageArray

{

   if (_imageArray==nil) {

       _imageArray =[NSMutableArrayarray];

    }

    return_imageArray;

}


- (NSMutableArray *)threadArray

{

    if (_threadArray==nil) {

       _threadArray =[NSMutableArrayarray];

    }

    return_threadArray;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    //加载视图

    [self_loadViews];

    

    //开启多线程加载图片

    [self_openMultiThread];    

}


//======加载这个,可以在主线程中加载

- (void) _loadViews

{

   for (int i=0;i<15; i++)

    {

       int col=i%3;

       int row=i/3;

        

       UIImageView *imageView=[[UIImageViewalloc] initWithFrame:CGRectMake(22+col*(80+45),40+row*(100+20),90, 90)];

        imageView.backgroundColor=[UIColorredColor];

        [self.imageArrayaddObject:imageView];

        

        [self.viewaddSubview:imageView];

    }

//    添加按钮

     UIButton * button=[UIButtonbuttonWithType:UIButtonTypeContactAdd];

     button.frame=CGRectMake(0,20, 20, 20);

     [button addTarget:selfaction:@selector(btClick)forControlEvents:UIControlEventTouchUpInside];

     [self.viewaddSubview:button];    

}


//开启多线程加载图片

- (void) _openMultiThread

{

   for (int i=0; i<self.imageArray.count; i++)

    {

//        [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:@(i)];

        

//        要存数组就不能使用类方法创建线程

       NSThread * thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(loadImage:)object:@(i)];

        [threadstart];

        [self.threadArrayaddObject:thread];

    }

}




- (void)loadImage:(NSNumber *) number

{

    //取到索引

   NSInteger index=[number integerValue];

   

    NSString * imageSrc=@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%d.jpg";

    

    imageSrc=[NSStringstringWithFormat:imageSrc,index];

    

    

   NSURL * URL=[NSURLURLWithString:imageSrc];

    NSData * data=[NSDatadataWithContentsOfURL:URL];

   UIImage *image=[UIImageimageWithData:data];

    

    //封装model

   ImageModel * model=[[ImageModelalloc] init];

    model.image=image;

    model.index=index;

    

   //如果状态为删除,则退出线程

   NSThread * thread = [NSThreadcurrentThread];

   if ([thread isCancelled])

    {

        [NSThreadexit];//执行exit,后边的语句不再执行。所以不用写return

    //return也可以退出进程,一旦退出就不能再使用start开启

    }

    NSLog(@"%@", thread);

    [selfperformSelectorOnMainThread:@selector(showImage:)withObject:model waitUntilDone:NO];

}


//回到主线程更新图片

- (void) showImage:(ImageModel *) model

{

   UIImageView * imageView=self.imageArray[model.index];

    imageView.image=model.image;

}



#pragma mark - 点击事件

- (void)btClick

{

//    当点击按钮时,对所有线程进行标记

   for (NSThread * tin self.threadArray)

    {

        [tcancel];

    }

}

0 0
原创粉丝点击