- (void)viewDidLoad {
[super viewDidLoad];
// _imageview=[[UIImageView alloc]init];
// _imageview.frame=CGRectMake(50, 50, 300, 300);
// _imageview.contentMode=UIViewContentModeScaleAspectFit;
UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(50, 600, 275, 30)];
[button setTitle:@"加载图片" forState: UIControlStateNormal];
[button addTarget:self action:@selector(loadImagewithThread) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState: UIControlStateHighlighted];
[self.view addSubview:button];
// [self.view addSubview:_imageview];
_tupianarray=@[@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=548095586,967520433&fm=116&gp=0.jpg",@"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2797161595,3723997864&fm=116&gp=0.jpg",@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1022628615,2842914551&fm=116&gp=0.jpg",@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1281570060,2936940376&fm=116&gp=0.jpg",@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3586233367,3171193232&fm=111&gp=0.jpg",@"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1759831704,749363101&fm=111&gp=0.jpg",@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=476197964,4156432555&fm=116&gp=0.jpg",@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=476197964,4156432555&fm=116&gp=0.jpg",@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=796806266,1831553124&fm=111&gp=0.jpg"];
[self layout];
[self loadImagewithThread];
}
-(void)layout
{
_mutable=[NSMutableArray array];
for (int row=0; row<3; row++)
{
for (int column=0; column<3; column++)
{
UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(18.75+column*118.75, 20+row*118.75, 100, 100)];
imageView.backgroundColor=[UIColor redColor];
[self.view addSubview:imageView];
[_mutable addObject:imageView];
}
}
}
#pragma mark 使用线程
-(void)loadImagewithThread
{
//创建多线程
for (int i=0;i<9;i++)
{
// [NSThread detachNewThreadSelector:@selector(myThread2:) toTarget:self withObject:[NSNumber numberWithInt:i]];
NSThread*thread=[[NSThread alloc]initWithTarget:self selector:@selector(myThread2:) object:[NSNumber numberWithInt:i]];
[thread start];
thread.name=[NSString stringWithFormat:@"%i",i];
}
}
#pragma mark 子线程 带参数的
-(void)myThread2:(NSNumber*)Index
{
if (![Index isEqual:@8]) {
//线程休眠2秒
[NSThread sleepForTimeInterval:2];
}
NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:_tupianarray[[Index intValue]]]];
NSArray *arr=@[data,Index];
[self performSelectorOnMainThread:@selector(updateImage:) withObject:arr waitUntilDone:YES];
NSLog(@"%@", [NSThread currentThread].name);
}
-(void)updateImage:(NSArray*)array
{
UIImage *image=[UIImage imageWithData:array[0]];
UIImageView*imageView=_mutable[[array[1] intValue]];
imageView.image=image;
}
//#pragma mark 不用线程的情况
//-(void)loadimage
//{
UIImage*image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://scimg.jb51.net/allimg/160815/103-160Q509544OC.jpg"]]];
_imange.image=image;
//
// //使用线程
// //方法一:使用对象方法
// NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(myThread) object:nil];
// [thread start];//启动一个线程并不意味着以及执行,而是出于就绪状态,当系统调度时才真正执行。
// //方法二 类方法
// [NSThread detachNewThreadSelector:@selector(myThread) toTarget:self withObject:nil];
//
//}
//#pragma mark 子线程中的操作
//-(void)myThread
//{
// NSLog(@"进线程了");
// NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://picm.photophoto.cn/005/008/017/0080170433.jpg"]];
UIImage*image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://scimg.jb51.net/allimg/160815/103-160Q509544OC.jpg"]]];
_imageview.image=image;
// //这个方法是NSobject的分类方法,每个NSobject对象都有这个方法
// [ self performSelectorOnMainThread:@selector(backmainThread:) withObject:data waitUntilDone:YES];
//
//}
//#pragma mark 回归主线程的一个更新ui的方法
//-(void)backmainThread:(NSData*)data
//{
// NSLog(@"h回归主项");
// _imageview.image=[UIImage imageWithData:data];
//}