//c++
// ViewController.m網絡
// NSThread_多線程併發多線程
//併發
// Created by dc008 on 15/12/24.ide
// Copyright © 2015年 崔曉宇. All rights reserved.spa
//.net
#import "ViewController.h"線程
#define ROW 53d
#define COLUMN 3orm
#define IMAGE_COUNT ROW*COLUMN
#define WIDTH 100 //圖片寬
#define HEIGHT WIDTH //圖片高
@interface ViewController ()
{
NSMutableArray *_imageViews;
NSArray *array;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
array = @[@"http://pic12.nipic.com/20110121/3556745_181933155140_2.jpg",
@"http://pic31.nipic.com/20130722/12196046_160624534194_2.jpg",
@"http://pic4.nipic.com/20091118/3047572_163336087407_2.jpg",
@"http://pic.nipic.com/2008-05-29/200852973848511_2.jpg",
@"http://pic41.nipic.com/20140504/18401726_095421197182_2.jpg",
@"http://pic51.nipic.com/file/20141024/13035204_155709166956_2.jpg",
@"http://pic36.nipic.com/20131125/8821914_113934565000_2.jpg",
@"http://pic31.nipic.com/20130722/12196046_160637651196_2.jpg",
@"http://pic28.nipic.com/20130412/3718408_231850588113_2.jpg",
@"http://pica.nipic.com/2007-06-06/20076615744388_2.jpg",
@"http://pic23.nipic.com/20120901/8737320_222046006000_2.jpg",
@"http://pic12.nipic.com/20110119/3556745_180736335138_2.jpg",
@"http://pic27.nipic.com/20130226/6806713_165327321179_2.jpg",
@"http://pic18.nipic.com/20111202/8920105_164704212146_2.jpg",
@"http://img.taopic.com/uploads/allimg/110914/8879-11091423505866.jpg"
];
}
- (void)layout{
_imageViews = [NSMutableArray array];
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,WIDTH , HEIGHT)];
imageView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:imageView];
[_imageViews addObject:imageView];
}
}
//添加按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 600, 275, 30);
[button setTitle:@"加載圖片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(useMultiThread2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)updateImage : (NSArray *)arr{
UIImage *image = [UIImage imageWithData:arr[0]];
UIImageView *imageView = _imageViews[[arr[1]intValue] ];
imageView.image = image;
}
- (void)loadImage : (NSNumber *)index {
int i = [index intValue];
if (![index isEqual: @14]) {
//若是當前進程不是最後一個,就休眠2秒
[NSThread sleepForTimeInterval:2];
}
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:array[i]]];
NSLog(@"%@",[NSString stringWithFormat:@"%@",array[i]]);
NSArray *arrData = @[data,index];
[self performSelectorOnMainThread:@selector(updateImage:) withObject:arrData waitUntilDone:YES];
}
- (void)useMultiThread{
for (int i; i < IMAGE_COUNT; i++) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
thread.name = [NSString stringWithFormat:@"個人線程:%d",i];
[thread start];
}
}
#pragma mark 改變線程優先級
//提升它被優先加載的概率,可是它頁未必就第一個加載。1.其餘進程是先啓動的 2.網絡情況咱們沒辦法修改
- (void)useMultiThread2{
NSMutableArray *threads = [NSMutableArray array];
for (int i=0; i<IMAGE_COUNT; i++) {
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
//設置進程優先級(範圍0-1)
if (i == IMAGE_COUNT - 1) {
thread.threadPriority = 1.0;
}
else{
thread.threadPriority = 0;
}
[threads addObject:thread];
}
for (int i = 0; i<IMAGE_COUNT; i++) {
NSThread *thread =threads[i];
[thread start];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end