ios - How do i save an image that was selected by a user with swift? -
in app making, user select photo, appear in imageview. how save image selected in image.xcassets folder, when relaunch app, image still there?
here code using...
import uikit class viewcontroller: uiviewcontroller, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate { @iboutlet weak var imageview: uiimageview! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func action(sender: anyobject) { if uiimagepickercontroller.issourcetypeavailable(uiimagepickercontrollersourcetype.camera) { let imagepicker:uiimagepickercontroller = uiimagepickercontroller() imagepicker.delegate = self imagepicker.sourcetype = uiimagepickercontrollersourcetype.camera imagepicker.allowsediting = false self.presentviewcontroller(imagepicker, animated: true, completion: nil) } } func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [nsobject : anyobject]) { self.dismissviewcontrolleranimated(true, completion: nil) self.imageview.image = info[uiimagepickercontrolleroriginalimage] as? uiimage //how save image in folder? } }
you can doing this
var finalimage : uiimage() //your method func saveimage () { self.imageview.image = info[uiimagepickercontrolleroriginalimage] as? uiimage self.finalimage = self.imageview.image uiimage let nsdocumentdirectory = nssearchpathdirectory.documentdirectory let nsuserdomainmask = nssearchpathdomainmask.userdomainmask if let paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, true) { if paths.count > 0 { if let dirpath = paths[0] as? string { let readpath = dirpath.stringbyappendingpathcomponent("image.png") let image = uiimage(named: readpath) let writepath = dirpath.stringbyappendingpathcomponent("image2.png") uiimagepngrepresentation(self.finalimage).writetofile(writepath, atomically: true) } } } }
Comments
Post a Comment