多线程基础知识

file:///Users/jerehedu/Desktop/屏幕快照%202015-06-02%2019.20.12.png

代码如下

 

#import "ViewController.h"

#import "UIImageView+ImageInit.h"

@interface ViewController ()

 

@property(nonatomic,strong)NSMutableArray * imageViewArray;

@property(nonatomic,strong)NSMutableDictionary * dic;

@property(nonatomic,strong)NSMutableArray * threadArray;

@property(nonatomic,assign)NSInteger count;

 

@end

 

@implementation ViewController

 

- (NSMutableArray *)imageViewArray{

    if (_imageViewArray==nil) {

        

        _imageViewArray=[NSMutableArray array];

        

    }

    return _imageViewArray;

}

- (NSMutableDictionary *)dic{

    if (_dic==nil) {

        _dic=[NSMutableDictionary dictionary];

    }return _dic;

}

- (NSMutableArray *)threadArray{

    if (_threadArray==nil) {

        _threadArray=[NSMutableArray array];

    }return _threadArray;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self _loadView];

    [self _openThread];

    

}

 

- (void)_loadView{

    

    UIButton * bt=[UIButton buttonWithType:UIButtonTypeCustom];

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

    bt.backgroundColor=[UIColor redColor];

    [bt addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bt];

    UIImageView * imageView=[[UIImageView alloc]init];

    

    self.imageViewArray=[imageView getImage];

    

    NSLog(@"%li",self.imageViewArray.count);

    

    /*

//    

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

//        int row=i/3;

//        int col=i%3;

//        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20+col*(45+80), row*(40+80)+40, 80, 80)];

//        imageView.backgroundColor=[UIColor redColor];

//        [self.view addSubview:imageView];

//        [self.imageViewArray addObject:imageView];

//        

//    }

     

    */

    

    

}

 

 

- (void)_openThread{

    

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

        //用分类去封装

        /*

       // NSInteger num=[index integerValue];

        

//        NSString * path=[NSString stringWithFormat:@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%i.jpg",i];

//       UIImageView * ima=[[UIImageView alloc]init];

//        [ima openThread:path];

     */

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

         [thread start];

        [self.threadArray addObject:thread];

        

       

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

        

    }

}

 

- (void)loadImage:(NSNumber *)index{

    

    //从网上加载图片

    NSInteger num=[index integerValue];

    

    NSString * path=[NSString stringWithFormat:@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%li.jpg",num];

  

     NSURL * url=[NSURL URLWithString:path];

     NSData * data=[NSData dataWithContentsOfURL:url];

    

 

    UIImage * image=[UIImage imageWithData:data];

    if (image!=nil) {

        

       [self.dic setObject:image forKey:@"image"];

       [self.dic setValue:index forKey:@"index"];

        

    }

    

    

    MjimageModel * mj=[MjimageModel imageModelWithImageDic:self.dic];

   /*

   // NSThread * thread=[NSThread currentThread];

//    

//    if ([thread isCancelled]) {

//        

//        [NSThread exit];

//        

//    }

    

   */

    [self performSelectorOnMainThread:@selector(toTheScreen:) withObject:mj waitUntilDone:NO];

 

    

}

 

 

 

 

- (void)toTheScreen:(MjimageModel *)mj{

    

    NSInteger i=mj.index;

    UIImage * image=mj.image;

    UIImageView * imageView=self.imageViewArray[i];

    imageView.image=image;

    [self.view addSubview:imageView];

    

}

 说明加上如下的内容。可以实现点击按钮的时候暂停的作用

//buttton的点击事件

- (void)click{

    

    

//    for (NSThread * thread in self.threadArray) {

//        [thread cancel];

//        

//    }

    

  }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。