ios - How do I get my Activity Indicator to start at the right time? -
from read can activate with button put right after button action starts http post , upload, right starts spinning right after function has finished, after upload complete, opposite of expect.
i'm new swift please explain answer nubie. many thanks.
@ibaction func upload(sender: anyobject) { myactivityindicator.startanimating() var imagedata = uiimagepngrepresentation(myimageview.image) if imagedata != nil{ var request = nsmutableurlrequest(url: nsurl(string:"http://www.example.com/upload.php")!) var session = nsurlsession.sharedsession() request.httpmethod = "post" var boundary = nsstring(format: "---------------------------14737809831466499882746641449") var contenttype = nsstring(format: "multipart/form-data; boundary=%@",boundary) // println("content type \(contenttype)") request.addvalue(contenttype, forhttpheaderfield: "content-type") var body = nsmutabledata.alloc() // title body.appenddata(nsstring(format: "\r\n--%@\r\n",boundary).datausingencoding(nsutf8stringencoding)!) body.appenddata(nsstring(format:"content-disposition: form-data; name=\"title\"\r\n\r\n").datausingencoding(nsutf8stringencoding)!) body.appenddata("hello world".datausingencoding(nsutf8stringencoding, allowlossyconversion: true)!) // image body.appenddata(nsstring(format: "\r\n--%@\r\n", boundary).datausingencoding(nsutf8stringencoding)!) body.appenddata(nsstring(format:"content-disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\r\n").datausingencoding(nsutf8stringencoding)!) // println("body=\(body)") body.appenddata(nsstring(format: "content-type: application/octet-stream\r\n\r\n").datausingencoding(nsutf8stringencoding)!) body.appenddata(imagedata) body.appenddata(nsstring(format: "\r\n--%@\r\n", boundary).datausingencoding(nsutf8stringencoding)!) request.httpbody = body var returndata = nsurlconnection.sendsynchronousrequest(request, returningresponse: nil, error: nil) var returnstring = nsstring(data: returndata!, encoding: nsutf8stringencoding) println("returnstring \(returnstring)") //self.myactivityindicator.stopanimating() //self.myimageview.image = nil } }
try this
myactivityindicator.startanimating() dispatch_async(dispatch_get_global_queue(int(qos_class_user_initiated.value), 0)) { //do post code here or call post func dispatch_async(dispatch_get_main_queue()) { myactivityindicator.stopanimating() } }
http://ijoshsmith.com/2014/07/29/a-swift-app-that-calls-json-web-services/ http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1
Comments
Post a Comment