Wednesday 15 August 2012

ios - Download multiple(Paralllel) files(.mp4) with progressbar and save it to gallery -



ios - Download multiple(Paralllel) files(.mp4) with progressbar and save it to gallery -

i have download multiple .mp4 videos , show progressbar each. have display these progress in tableview. know how download single video , know how save gallery using ...

currently using code..

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nslog(@"downloading started"); nsstring *urltodownload = @"http://original.mp4"; nsurl *url = [nsurl urlwithstring:urltodownload]; nsdata *urldata = [nsdata datawithcontentsofurl:url]; if ( urldata ) { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"thefile.mp4"]; //saving done on main thread dispatch_async(dispatch_get_main_queue(), ^{ [urldata writetofile:filepath atomically:yes]; nslog(@"file saved !"); }); } });

first approach

first of how can show progress while downloading using above code. then don't know downloading. want know path above code save .mp4 video , want modify(save in gallery). i want show download progress each video.

second approach

i think have utilize nsoperationqueue run downloads asynchronously, allow number done in parallel,etc . don't know how implement progress ..

i created had download multiple tasks , shows progress of each on too: http://github.com/manavgabhawala/caen-lecture-scraper (this mac) need create custom uitableviewcell class has progress bar within of , iboutlet bar. create class of cell conform nsurlsessiondownloaddelegate progress comes in. further, used semaphore ensure 3 downloads happened simultaneously. here's class convenience:

let downloadsallowed = dispatch_semaphore_create(3) class progresscellview: uitableviewcell { @iboutlet var progressbar: uiprogressview! @iboutlet var statuslabel: uilabel! var downloadurl : nsurl! var savetopath: nsurl! func setup(downloadurl: nsurl, savetopath: nsurl) { self.downloadurl = downloadurl self.savetopath = savetopath statuslabel.text = "waiting queue" // optionally phone call begin download here if don't want wait after queueing cells. } func begindownload() { statuslabel.text = "queued" allow session = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration(), delegate: self, delegatequeue: nsoperationqueue()) allow task = session.downloadtaskwithurl(downloadurl)! dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), { dispatch_semaphore_wait(downloadsallowed, dispatch_time_forever) dispatch_async(dispatch_get_main_queue(), { self.statuslabel.text = "downloading" }) task.resume() }) } } extension progresscellview : nsurlsessiondownloaddelegate { func urlsession(session: nsurlsession, downloadtask: nsurlsessiondownloadtask, didwritedata byteswritten: int64, totalbyteswritten: int64, totalbytesexpectedtowrite: int64) { progressbar.setprogress(float(totalbyteswritten) / float(totalbytesexpectedtowrite), animated: true) } func urlsession(session: nsurlsession, downloadtask: nsurlsessiondownloadtask, didfinishdownloadingtourl location: nsurl) { // todo: write file downloaded. // re-create file `location` `savetopath` , handle errors necessary. dispatch_semaphore_signal(downloadsallowed) dispatch_async(dispatch_get_main_queue(), { self.statuslabel.text = "downloaded" }) } func urlsession(session: nsurlsession, didbecomeinvalidwitherror error: nserror?) { print(error!.localizeddescription) // todo: error handling. } }

change creation of semaphore value whatever want (the number of concurrent downloads allowed). note must phone call setup on cell downloadurl , savetopath after instantiating it. if want queue , download videos phone call begindownload() within of setup(). otherwise, phone call on each , every cell whenever want start downloading videos.

ios objective-c grand-central-dispatch nsoperationqueue

No comments:

Post a Comment