IOS multithreading _ NSThread and NSInvocationOperation
// Although most of the threads are currently using GCD, we should be familiar with other methods to introduce the simple usage of NSThread and NSInvocationOperation.
@ InterfaceyxpViewController ()
{
UIImageView * _ imageView;
// Declare a queue
NSOperationQueue * _ operationQueue;
}
@ End
@ Implementation yxpViewController
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// Initialize the operation queue
_ OperationQueue = [[NSOperationQueuealloc] init];
// This limits the queue to run only one thread at a time
[_ OperationQueuesetMaxConcurrentOperationCount: 1];
// Initialize an _ ImageView
_ ImageView = [[UIImageViewalloc] initWithFrame: CGRectMake (300,450,)];
_ ImageView. backgroundColor = [UIColorgrayColor];
_ ImageView. animationDuration = 3.0;
_ ImageView. animationRepeatCount = 0;
[Self. viewaddSubview: _ imageView];
// The following two methods add a thread
// First
// Create an NSInvocationOperation object and initialize it to the Method
// Here, the value after the selector parameter is the Method (function, Method) You want to run in another thread)
// Here, the object value is the data to be passed to the previous method.
NSInvocationOperation * theOp = [[NSInvocationOperationalloc] initWithTarget: selfselector: @ selector (addTaskMethod :) object: _ imageView];
// Of course, you can also choose to directly call the start method, but this sample is synchronous, that is, the main thread must be executed after the background thread execution is complete.
// [TheOp start];
// Operation is added to the queue for theOp and the main thread to execute at the same time, that is, the asynchronous thread, in order to achieve our results
[_ OperationQueueaddOperation: theOp];
// Type 2
// Use [NSThread detachNewThreadSelector: @ selector (addTaskMethod :) toTarget: self withObject: nil]; you can also add a thread which is equivalent to the previous one.
// [NSThread detachNewThreadSelector: @ selector (addTaskMethod :) toTarget: self withObject: nil];
}
// Method for running another thread
-(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;
// Return to the main thread for calling methods related to the UI thread (main thread) that cannot be called in a non-thread. You can call the method directly to check whether the method is valid.
[Self?mselec=mainthread: @ selector (startImageViewAnimating) withObject: nilwaitUntilDone: YES];
}
-(Void) startImageViewAnimating
{
[_ ImageView startAnimating];
}
// Method for downloading images
-(UIImage *) getImageFromURL :( NSString *) fileURL {
NSLog (@ "execute image download function ");
UIImage * result;
NSData * data = [NSDatadataWithContentsOfURL: [NSURLURLWithString: fileURL];
Result = [UIImageimageWithData: data];
Return result;
}
-(Void) didReceiveMemoryWarning
{
[SuperdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@ End