IOS-UIImageView: loading images downloaded from the Network (asynchronous + multithreading)
The most primitive way to load images downloaded from the network:
// The original method for loading network images is equivalent to blocking the main thread. The interface is stuck-(void) setImageWithURL :( NSString *) imageDownloadUrl {UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (44, 64,250,250)]; NSURL * URL = [NSURL URLWithString: imageDownloadUrl]; NSError * ERROR; NSData * imageData = [NSData dataWithContentsOfURL: URL options: NSDataReadingMappedIfSafe error: & ERROR]; UIImage * image = [UIImage imageWithData: imageData]; [imageView setImage: image];}
Use an asynchronous thread to load images. After loading, set images. Before network loading, UIimageview uses placeholder images.
// Asynchronous threads load network download images --> return to the main thread to update the UI-(void) downloadImageWithUrl :( NSString *) imageDownloadURLStr {// to use _ block UIImage * image = [[UIImage alloc] init]; // image download link NSURL * imageDownloadURL = [NSURL URLWithString: imageDownloadURLStr]; // download the image to an asynchronous thread // create an asynchronous thread to execute the queue maid ("imageDownloadQueue", NULL); // create an asynchronous thread dispatch_async (asynchronousQueue, ^ {// NSData format NSError * error; NSData * imageData = [NSData dataWithContentsOfURL: imageDownloadURL options: NSDataReadingMappedIfSafe error: & error]; if (imageData) {image = [UIImage imageWithData: imageData];} // return to the main thread to update UI dispatch_async (dispatch_get_main_queue (), ^ {[_ imageView setImage: image] ;}) ;}
To enable the automatic release pool for thread security considerations, use the same method as above:
# Pragma mark-download image-Sub-thread call-(void) downloadImage {/** the runloop in the sub-thread is disabled by default, which means that the auto release pool is not automatically created, the autorelease object in the Child thread will not be released by the Child pool. In this case, the memory may leak due to the failure to release the cotton at the same position. Therefore, you need to manually create */@ autoreleasepool {NSLog (@ "% @", [NSThread currentThread]). NSURL * url = [NSURL URLWithString: @ "http://baidu.com/image/Users/qiuxuewei/Desktop/qiu.JPG"]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * image0 = [UIImage imageWithData: data]; UIImage * image = [UIImage imageNamed: @ "qiu. JPG "]; // UI is required in the main thread // self. imageView. image = image; // 1. [self defined mselecw.mainthread: @ selector (updataImage :) withObject: image waitUntilDone: NO]; // 2. [self defined mselector: @ selector (updataImage :) onThread: [NSThread mainThread] withObject: image waitUntilDone: YES]; [self. imageView complete mselecw.mainthread: @ selector (updataImage :) withObject: image waitUntilDone: YES]; // waitUntilDone: Indicates whether to wait until the sub-thread method is executed. // If YES: then wait until the sub-thread method is executed and then execute the current function NSLog (@ "done .. ") ;}}-(void) updataImage :( UIImage *) image {self. imageView. image = image ;}