Nsthread Downloading images asynchronously
There are three scenarios for dealing with multithreading in iOS, Nsthread, Nsoperation, GCD. Of course GCD should be the most convenient, we learn one by one. First understand the bottom, and finally use the most convenient.
Nsthread:
Pros: Nsthread than the other two lightweight
Cons: You need to manage your thread's life cycle, thread synchronization. Thread synchronization has a certain overhead in locking data
Let's start with the NSThread
asynchronous loading of network images:
There are two ways to create a nsthread, one is through an instance method. One is through a class method.
NSThreadself"doSomeThing"nilNSThread.detachNewThreadSelector("doSomeThing1"selfnil);
Auto-open through the class method, created by the instance method to manually invoke the start()
method to open
let "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png" let "https://avatars2.githubusercontent.com/u/8502419?v=3&s=400"
Here we first define two graphs from the network
weakvarweakvar imageView: UIImageView!
Then two controls are applied to the interface, one UIImageView
for displaying the picture, and one for UIActivityIndicatorView
displaying the load and turning the circle.
And then get two more buttons. No, the following source is attached.
In the Viewdidload method, set the activity.hidden = true
activity control to be hidden by default.
The code of the first button click event
activity.hiddenfalse activity.startAnimating() NSThread.detachNewThreadSelector("downLoadImage"selfnil);
Show controls, turn on animations, open a thread
selector :线程执行的方法,这个selector只能有一个参数,而且不能有返回值。 target :selector消息发送的对象 withObject:传输给target的唯一参数,也可以是nil
This is an explanation of the three parameters.
Func downloadimage () {print ("I want to download the picture.")if Letwr.=Nsurl (string: picaddress) {if Let Data =NSData (Contentsofurl:url) { LetImg=UIImage (Data:Data)if(img!=Nil) {//There is no such method in Swift for the time being. Performselectoronmainthread, then use the GCD method first.Dispatch_async (Dispatch_get_main_queue ()) { Self.ImageView.Image=Img!; Self.Activity.Hidden=truePrint"The picture is finished downloading.") } }Else{Print ("Baidu") } }Else{Print ("I didn't get the picture from the URL.") } }Else{Print ("I didn't get the URL.") } }
This is the thread implementation method, after downloading to the data through the main thread to update the UI
Another way to button
activity.=false activity.startAnimating() letthread=self"downLoadImage1:", object: picAddress1); thread.start();
Here is a parameter, in fact two can use a method. A different address is all right, here to learn more. It was tested in two ways.
Func DownLoadImage1 (str:String) {print ("I want to download the picture.")if LetUrl=Nsurl (string: STR) {if Let Data =NSData (Contentsofurl:url) { LetImg=UIImage (Data:Data)if(img!=Nil) {//There is no such method in Swift for the time being. Performselectoronmainthread, then use the GCD method first.Dispatch_async (Dispatch_get_main_queue ()) { Self.ImageView.Image=Img!; Self.Activity.Hidden=truePrint"The picture is finished downloading.") } }Else{Print ("Baidu") } }Else{Print ("I didn't get the picture from the URL.") } }Else{Print ("I didn't get the URL.") } }
This is the second button of a method
The effect is to click a button, turn the circle wait. Then the picture is displayed, the other point is waiting and then the picture is displayed.
Xcode 6, NSData can get most network pictures, XCode 7 Beta has a lot of pictures to get. Estimation is a security issue. There is no good way to find it now.
I'm using the Xcode 7 Beta code to attach it.
: Swift asynchronously obtains network pictures
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift detailed 14-----------nsthread asynchronous Download image