i creating simple video downloading app
everything work when click on download button video downloading work activity indicator not work , write msg in println not work
i add button on custom view , used swiftspinner.swift library activity indicator
here code
@ibaction func downloadaction(sender: anyobject) { println("hello") swiftspinner.show("downloading...", animated: true) let urlpath = self.photoinfo?.sourceimageurl let urllastpath = self.photoinfo?.sourceimageurl.lastpathcomponent var data : nsdata = nsdata(contentsofurl: urlpath!)! var path = nssearchpathfordirectoriesindomains(nssearchpathdirectory.cachesdirectory, nssearchpathdomainmask.userdomainmask, true)[0].stringbyappendingpathcomponent(urllastpath!) string println(path) delay(seconds: 16.0, completion: { swiftspinner.show("downloading completed", animated: false) }) data.writetofile(path, atomically: true) delay(seconds: 18.0, completion: { swiftspinner.hide() }) uisavevideoatpathtosavedphotosalbum(path, nil, nil, nil) }
here complete working code:
import uikit class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } @ibaction func downloadaction(sender: anyobject) { println("hello") //spinner show when press button. swiftspinner.show("downloading...", animated: true) if let checkedurl = nsurl(string: "http://hdwallpaperd.com/wp-content/uploads/hd-widescreen-wallpapers-4.jpg") { downloadimage(checkedurl) } } func downloadimage(url:nsurl){ println("started downloading \"\(url.lastpathcomponent!.stringbydeletingpathextension)\".") getdatafromurl(url) { data in // stuff background queue dispatch_async(dispatch_get_main_queue()) { swiftspinner.show("downloading completed", animated: false) //here can set delay can perform action after download finish. let delaytime = dispatch_time(dispatch_time_now, int64(2 * double(nsec_per_sec))) dispatch_after(delaytime, dispatch_get_main_queue()) { // code run after 2 second in case. var path = nssearchpathfordirectoriesindomains(nssearchpathdirectory.cachesdirectory, nssearchpathdomainmask.userdomainmask, true)[0].stringbyappendingpathcomponent("image1") string println(path) data!.writetofile(path, atomically: true) swiftspinner.hide() // after 2 second spinner hide. uisavevideoatpathtosavedphotosalbum(path, nil, nil, nil) } } } } func getdatafromurl(url:nsurl, completion: ((data: nsdata?) -> void)) { nsurlsession.sharedsession().datataskwithurl(url) { (data, response, error) in completion(data: nsdata(data: data)) }.resume() } }
you can refer this example project.
Comments
Post a Comment