ios - No such file location - Uploading image from Swift to S3 -
i trying upload image amazon s3 getting following error:
optional(error domain=nscocoaerrordomain code=260 "the file “asset.jpg” couldn’t opened because there no such file." userinfo={nsfilepath=/asset.jpg, nsunderlyingerror=0x7fb0d8eb3b00 {error domain=nsposixerrordomain code=2 "no such file or directory"}})
the below code getting image or videos file path - stored in variable 'filelocation':
func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]) { //let mediatype2 : uiimage = info[uiimagepickercontrolleroriginalimage] as! uiimage let mediatype : cfstring = info[uiimagepickercontrollermediatype] as! cfstring //if video - save if mediatype == kuttypemovie { let path = (info[uiimagepickercontrollermediaurl] as! nsurl).path if uivideoatpathiscompatiblewithsavedphotosalbum(path!) && savevideovar == true{ uisavevideoatpathtosavedphotosalbum(path!, self, "video:didfinishsavingwitherror:contextinfo:", nil) } } else{ let img : uiimage = info[uiimagepickercontrolleroriginalimage] as! uiimage let screensize: cgrect = uiscreen.mainscreen().bounds var multiplynum = screensize.width / img.size.width //if image height going more 60% of screen, resize width , height ensure isn't greater 60% while keeping aspect ratio correct if ((img.size.height*multiplynum) > (screensize.height*0.6)){ multiplynum = screensize.height*0.6 / img.size.height imageviewwidthconstraint.constant = (multiplynum*img.size.width) imageviewheightconstraint.constant = screensize.height*0.6 } else{ imageviewwidthconstraint.constant = screensize.width imageviewheightconstraint.constant = (multiplynum*img.size.height) } imageview.image = img } let filelocation: nsurl = info["uiimagepickercontrollerreferenceurl"] as! nsurl nwyt.uploads3(filelocation) self.dismissviewcontrolleranimated(true, completion: {}) }
no matter photo chose, gives same error file "asset.jpg" couldn't opened..." though when print filelocation, path more complex that, example: "assets-library://asset/asset.jpg?id=ec41ac54-cebd-49aa-a1fa-864370d103c0&ext=jpg"
implementation of nwyt.uploads3() :
func uploads3(filebody : nsurl){ let uploadrequest: awss3transfermanageruploadrequest = awss3transfermanageruploadrequest() uploadrequest.bucket = "bx-video" uploadrequest.key = "testobject.jpg" uploadrequest.body = filebody print(filebody) let transfermanager: awss3transfermanager = awss3transfermanager.defaults3transfermanager() transfermanager.upload(uploadrequest).continuewithexecutor(awsexecutor.mainthreadexecutor(), withblock: {(task: awstask) -> anyobject! in if task.error != nil { print(task.error) nslog("error uploading : " + uploadrequest.key!) } else { nslog("upload completed : " + uploadrequest.key!) } return nil }) }
solution below:
if let img : uiimage = imageview.image! uiimage{ let path = (nstemporarydirectory() nsstring).stringbyappendingpathcomponent("image.png") let imagedata: nsdata = uiimagepngrepresentation(img)! imagedata.writetofile(path string, atomically: true) // once image saved can use path create local fileurl let url:nsurl = nsurl(fileurlwithpath: path string) nwyt.uploads3(url) }
Comments
Post a Comment