码迷,mamicode.com
首页 > 编程语言 > 详细

多线程基础知识

时间:2015-06-02 21:27:45      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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];

//        

//    }

    

  }

 

多线程基础知识

标签:

原文地址:http://www.cnblogs.com/yy515700/p/4547480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!