OBjective-c NSThread 多线程1




#import "ViewController.h"

#define ROW 5

#define COLUMN 3

#define IMAGR_COUNT ROW*COLUMN

#define W 100

#define H W


@interface ViewController ()

{

    NSMutableArray *muarray;

    NSArray * arrayWANGZHI;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    arrayWANGZHI=@[

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://pic19.nipic.com/20120310/8061225_093309101000_2.jpg",

                             @"http://img.tuku.cn/file_big/201409/048f4c825de5442e952ddc5e28563100.jpg",

                             @"http://p7.qhimg.com/t01308022902bb3cc15.jpg",

                             @"http://www.33lc.com/article/UploadPic/2012-8/2012815167420064.jpg",

                             @"http://d.3987.com/jmfghjgqbz.120925/004.jpg",

                             

                             @"http://img.bbs.duba.net/month_1011/1011020907c8b129640ca5a7e7.jpg",

                             

                             @"http://www.deskcar.com/desktop/car/201337162241/6.jpg",

                             @"http://www.deskcar.com/desktop/car/201337162241/6.jpg",

                             @"http://pic11.nipic.com/20101117/778850_171649021175_2.jpg",

                             @"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",

                             

                             @"http://kuoo8.com/wall_up/hsf2288/200911/200911031144096643.jpg",

                             @"http://pic.4j4j.cn/upload/pic/20130305/6399c64adb.jpg",

                             

                             @"http://www.deskcar.com/desktop/car/2005/2007121201442/10.jpg"

                             

                             ];


    

    

    [self layout];

    

}

-(void)layout{

    muarray=[[NSMutableArray alloc]initWithCapacity:10];

    for (int r=0; r<COLUMN; r++) {

        for (int c=0; c<ROW; c++) {

            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+r*118.75, 20+c*118.75, W, H)];

            [self.view addSubview:imageView];

            imageView.backgroundColor=[UIColor orangeColor];

            [muarray addObject:imageView];

            

        }

    }

    UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(150, 600, 100, 20)];

    [button setTitle:@"加载图片" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    

    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

    [button addTarget:self action:@selector(xiazaiXianCheng) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    

    

}

-(void)updateImage:(NSArray*)array{

    UIImage * image=[UIImage imageWithData:array[0]];

    UIImageView * imageViewI=muarray[[array[1] intValue]];

    imageViewI.image=image;

    

    

    

}

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

    NSLog(@"%@",[NSThread currentThread].name);

    //让最后一个线程优先执行,让其他线程延迟两秒

    if (![index isEqual:@14]) {

        [NSThread sleepForTimeInterval:2];

    }

    NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:arrayWANGZHI[[index intValue]]]];

    NSLog(@"%@", arrayWANGZHI[[index intValue]]);

    NSArray * dataArray=@[data,index];

    

    [self performSelectorOnMainThread:@selector(updateImage:) withObject:dataArray waitUntilDone:YES];

   

    

    

}

-(void)xiazaiXianCheng{

    for (int i=0; i<IMAGR_COUNT; i++) {

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

        thread.name=[NSString stringWithFormat:@"我的线程 %d",i];

        [thread start];//启动线程

        

    }

}


#pragma mark 改变线程优先级

//提高他被优先加载的几率,但是未必就第一个加载。1其他进程是先启动的2网络状况我们没有办法修改

-(void)sueMultiThread{

    NSMutableArray * muarray2=[[NSMutableArray alloc]initWithCapacity:10];

    

    for (int a=0; a<IMAGR_COUNT; a++) {

        NSThread * thread2=[[NSThread alloc]initWithTarget:self selector:@selector(gengxinImage:) object:[NSNumber numberWithInt:a]];

        if (a==IMAGR_COUNT-1) {

            thread2.threadPriority=1.0;

            

        }else{

            thread2.threadPriority=0;

            

        }

        

        [muarray2 addObject:thread2];

    }

    

    for (int s=0; s<IMAGR_COUNT; s++) {

        NSThread * thread3=muarray2[s];

        [thread3 start];

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    

  

    

}


@end


转载于:https://my.oschina.net/u/2483781/blog/550621

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值