ios - Download image directly to a photo album -


currently i'm using alamofire network requests. how can download image directly photo album exists using phphotolibrary photos.framework?

p.s.: don't mind have solution using nsurlsession itself.

considerations: file can't stored in disk temporarily. want data in memory , save once disk using photos.framework.

i figure out myself. implemented datataskwithrequest nsurlsession.

initially thought using nsdata(contentsofurl:) contentsofurl method not return until entire file transferred. should used local files , not remote ones because if you're loading large content , device has slow connection ui blocked.

// avoid this! let originalphotodata = nsdata(contentsofurl: nsurl(string: "http://photo.jpg")!) 

so, possible load content nsdata data task. 1 solution can like:

let request = nsmutableurlrequest(url: nsurl(string: urlstring.urlstring)!) request.allhttpheaderfields = [     "content-type": "image/jpeg" ] let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in     if let e = error {         print(e.description)         return     }     guard let image = uiimage(data: data) else {         print("no image")         return     }     // success     // note: not save album. removed code more concise.     phphotolibrary.sharedphotolibrary().performchanges {         let creationrequest = phassetchangerequest.creationrequestforassetfromimage(image)     }, completionhandler: { (success : bool, error : nserror?) -> void in         if success == false, let e = error){             print(e)         }     } } task.resume() 

the data available in final stage can instead access data , see progress using session delegate.

class session: nsobject, nsurlsessiondelegate, nsurlsessiontaskdelegate, nsurlsessiondatadelegate {      let configuration = nsurlsessionconfiguration.defaultsessionconfiguration()     let queue = nsoperationqueue.mainqueue()     var session = nsurlsession()      var buffer = nsmutabledata()     var expectedcontentlength = 0      override init() {         super.init()         session = nsurlsession(configuration: configuration, delegate: self, delegatequeue: queue)     }      func datataskwithrequest(request: nsurlrequest) -> nsurlsessiondatatask {         // delegate called if not specify completion block!         return session.datataskwithrequest(request)     }      func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceiveresponse response: nsurlresponse, completionhandler: (nsurlsessionresponsedisposition) -> void) {         buffer = nsmutabledata()         expectedcontentlength = int(response.expectedcontentlength)         print("expected content length:", expectedcontentlength)         completionhandler(nsurlsessionresponsedisposition.allow)     }      func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceivedata data: nsdata) {         buffer.appenddata(data)         let percentagedownloaded = float(buffer.length) / float(expectedcontentlength)         print("progress: ", percentagedownloaded)     }      func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?) {         print("finished with/without error \(error)")         // use buffer create uiimage     }  } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -