IOS多線程_NSThread和NSInvocationOperation

來源:互聯網
上載者:User

IOS多線程_NSThread和NSInvocationOperation

//雖然現在在多線程方面大多都在用GCD,當其他方式我們還是應該有所瞭解,給大家介紹一下NSThread和NSInvocationOperation的簡單用法


@interfaceyxpViewController ()

{

UIImageView *_imageView;

//聲明一個隊列

NSOperationQueue *_operationQueue;

}


@end


@implementation yxpViewController


- (void)viewDidLoad

{

[superviewDidLoad];

//初始化操作隊列

_operationQueue = [[NSOperationQueuealloc]init];

//在這裡限定了該隊列只同時運行一個線程

[_operationQueuesetMaxConcurrentOperationCount:1];

//初始化一_ImageView

_imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(10,70,300, 450)];

_imageView.backgroundColor=[UIColorgrayColor];

_imageView.animationDuration=3.0;

_imageView.animationRepeatCount=0;

[self.viewaddSubview:_imageView];

//下面兩種方法添加一個線程

//第一種

//建立一個NSInvocationOperation對象,並初始化到方法

//在這裡,selector參數後的值是你想在另外一個線程中啟動並執行方法(函數,Method)

//在這裡,object後的值是想傳遞給前面方法的資料

NSInvocationOperation* theOp = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(addTaskMethod:)object:_imageView];

//當然也可以選擇直接調用start方法開始,不過這個樣做是同步的,也就是說還是要等後台線程執行完成才執行主線程

//[theOp start];

// Operation加入到隊列中 是theOp和主線程同時執行,也就是非同步線程,這樣才能達到我們的效果

[_operationQueueaddOperation:theOp];

//第二種

//用[NSThread detachNewThreadSelector:@selector(addTaskMethod:) toTarget:self withObject:nil]; 也可以增加一個線程 等效於上一種

//[NSThread detachNewThreadSelector:@selector(addTaskMethod:) toTarget:self withObject:nil];


}

// 運行另外一個線程的方法

- (void)addTaskMethod:(id)data

{

NSString *url1=@"http://h.hiphotos.baidu.com/image/w%3D230/sign=b2d5c289123853438ccf8022a311b01f/91ef76c6a7efce1b1ae9f92fad51f3deb58f6510.jpg";

NSString *url2=@"http://h.hiphotos.baidu.com/image/pic/item/d058ccbf6c81800aae834e8bb33533fa838b47d5.jpg";

NSString *url3=@"http://d.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494eec3ba65132ff5e0fe99257e1b.jpg";

NSString *url4=@"http://g.hiphotos.baidu.com/image/pic/item/a6efce1b9d16fdfa81f4ace4b68f8c5494ee7b1b.jpg";

NSString *url5=@"http://g.hiphotos.baidu.com/image/pic/item/d6ca7bcb0a46f21f70031fdbf4246b600c33ae07.jpg";

NSArray *array=[[NSArrayalloc]initWithObjects:url1,url2,url3,url4,url5,nil];

NSMutableArray *imageArray=[[NSMutableArrayalloc]initWithCapacity:20];

for (NSString *stringin array) {

UIImage *image=[selfgetImageFromURL:string];

[imageArrayaddObject:image];

}

_imageView.animationImages=imageArray;

//回到主線程調用 在非線程中不能調用有關UI線程(主線程)的方法的,你可以直接調用一下看是否有效

[selfperformSelectorOnMainThread:@selector(startImageViewAnimating)withObject:nilwaitUntilDone:YES];

}

- (void)startImageViewAnimating

{

[_imageView startAnimating];

}

//下載圖片的方法

-(UIImage *) getImageFromURL:(NSString *)fileURL {

NSLog(@"執行圖片下載函數");

UIImage * result;

NSData * data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:fileURL]];

result = [UIImageimageWithData:data];

return result;

}


- (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.