ios - Swift 3 upload Video to server -
i trying upload video server. can upload images fine when trying upload video have no clue on how approach this. use following uploading images:
at "let image" throws error when select video album.
func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) { let imageurl = info[uiimagepickercontrollerreferenceurl] as! nsurl let imagename = imageurl.lastpathcomponent let documentdirectory = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true).first! let photourl = nsurl(fileurlwithpath: documentdirectory) let localpath = photourl.appendingpathcomponent(imagename!) let image = info[uiimagepickercontrolleroriginalimage]as! uiimage let data = uiimagepngrepresentation(image)
if want upload small video 20 30mb can try code. if want upload large video suggest upload video on amazon server.
first convert video url nsdata.
func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [anyhashable: any]) { var mtype: string? = (info.value(forkey: uiimagepickercontrollermediatype) as? string) if (mtype == "public.movie") { var selectedvideourl: url? = (info["uiimagepickercontrollermediaurl"] as? url) var videodata = data(contentsof: selectedvideourl) uploadvideo(videodata) } }
then upload that.
func uploadvideo(_ videodata: data) { var dictparam = [anyhashable: any]() var requestserializer = afhttprequestserializer() var responseserializer = afhttpresponseserializer() requestserializer = afjsonrequestserializer() responseserializer = afjsonresponseserializer() dictparam["title"] = txtvideotitle.text var error: error? var request: nsmutableurlrequest? = try? requestserializer.multipartformrequest(withmethod: "post", urlstring: "yourserverurl", parameters: dictparam, constructingbodywithblock: {(_ formdata: afmultipartformdata) -> void in if videodata != nil { formdata.appendpart(withfiledata: videodata, name: "video", filename: strvideofilepath.lastpathcomponent, mimetype: "video/quicktime") } }) var manager = afurlsessionmanager(sessionconfiguration: urlsessionconfiguration.default) var uploadtask: urlsessionuploadtask? uploadtask = manager.uploadtask(withstreamedrequest: request, progress: {(_ uploadprogress: progress) -> void in dispatchqueue.main.async(execute: {() -> void in //update progress view print("video uploading progress = \(uploadprogress.fractioncompleted)") }) }, completionhandler: {(_ response: urlresponse, _ responseobject: any?, _ error: error?) -> void in if error != nil { print("error while upload video on server = \(error?.localizeddescription)") } else { if cint(responseobject["status"]) == 1 { sharedclass.showalert("video uploaded successfully.") } else { sharedclass.showalert(responseobject["message"]) } } }) uploadtask?.resume() }
i hope you.
Comments
Post a Comment