ios - Download and Save Video to Camera Roll -
this seems should straight forward based on examples , docs i've read, still unable work strange reason.
i using alamofire frame work download video instagram. once downloaded, want save video camera roll. here code download video , save disk:
let destination: (nsurl, nshttpurlresponse) -> (nsurl) = { (temporaryurl, response) in if let directoryurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask)[0] as? nsurl { let finalpath = directoryurl.urlbyappendingpathcomponent("\(scripts.datetostring2(date: nsdate())).\(response.suggestedfilename!)") instagramengine.downloadmediapath = finalpath scripts.log("final path >> \(finalpath)") return finalpath } return temporaryurl } let request = alamofire.download(.get, self.videourl, destination) request.response { _, response, data, error in nsnotificationcenter.defaultcenter().postnotificationname(kmediadownloadcomplete, object: nil) }
once download complete, notification triggered calls function save camera roll:
uisavevideoatpathtosavedphotosalbum(instagramengine.downloadmediapath.urlstring, self, selector("video:didfinishsavingwitherror:contextinfo:"), nil)
everything being called based on log statements , no errors occured. didfinishsavingwitherror being called uisavevideoatpathtosavedphotosalbum , confirmed no errors found. when go check camera roll, still see no video saved there. ideas?
unfortunately there bug related uisavevideoatpathtosavedphotosalbum
, format mp4
, format used instagram.
there helper method called uivideoatpathiscompatiblewithsavedphotosalbum
indicate whether video compatible method uisavevideoatpathtosavedphotosalbum
. returns false
video downloaded instagram.
luckily still possible store videos camera roll. possible using alassetslibrary
. i've tried take sample code , adapt use alassetslibrary
, can working.
import assetslibrary ... ... func downloadvideotocameraroll() { // local variable pointing local file path downloaded video var localfileurl: string? // closure generating local file path downloaded video. pointing documents directory unique udid file name. let destination: (nsurl, nshttpurlresponse) -> (nsurl) = { (temporaryurl, response) in if let directoryurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask)[0] as? nsurl { let finalpath = directoryurl.urlbyappendingpathcomponent("\(nsuuid()).\(response.suggestedfilename!)") localfileurl = finalpath.absolutestring return finalpath } return temporaryurl } // media post should downloaded let posturl = nsurl(string: "https://api.instagram.com/v1/media/" + "952201134785549382_250131908" + "?access_token=" + instagramengine.sharedengine().accesstoken)! // magic happens turns posturl videourl, actual url of video media: let videourl = nsurl(string: "https://scontent.cdninstagram.com/hphotos-xfp1/t50.2886-16/11104555_1603400416544760_416259564_s.mp4")! // download starts let request = alamofire.download(.get, videourl, destination) // completion handler download request.response { (request, response, data, error) -> void in if let path = localfileurl { let isvideocompatible = uivideoatpathiscompatiblewithsavedphotosalbum(path) println("bool: \(isvideocompatible)") // logs out "bool: false" let library = alassetslibrary() library.writevideoatpathtosavedphotosalbum(nsurl(string: path), completionblock: { (url, error) -> void in // done! go check camera roll }) } } }
Comments
Post a Comment