ios - UIImagePickerController image in Swift -


i using uiimagepickercontroller in program , changing image of imageview have added. however, whenever restart app , come home screen, automatically resetting default image had before, rather user selected image. how can make records image last used, , reloads every time program starts?

 var imagepicker = uiimagepickercontroller()  func chooseimage(_ sender: any) { //function called button press      let imagepickercontroller = uiimagepickercontroller()     imagepickercontroller.delegate = self     imagepickercontroller.allowsediting = true      let actionsheet = uialertcontroller(title: "photo source", message: "choose source", preferredstyle: .actionsheet)      actionsheet.addaction(uialertaction(title: "camera", style: .default, handler: { (action:uialertaction) in          if uiimagepickercontroller.issourcetypeavailable(.camera) {             imagepickercontroller.sourcetype = .camera             self.present(imagepickercontroller, animated: true, completion: nil)         }else{             print("camera not available")         }       }))      actionsheet.addaction(uialertaction(title: "photo library", style: .default, handler: { (action:uialertaction) in         imagepickercontroller.sourcetype = .photolibrary         self.present(imagepickercontroller, animated: true, completion: nil)     }))      actionsheet.addaction(uialertaction(title: "default", style: .default, handler: { (action:uialertaction) in         self.avatarimageview.image = uiimage(named: "avatar.png")       }))      actionsheet.addaction(uialertaction(title: "cancel", style: .cancel, handler: nil))      self.present(actionsheet, animated: true, completion: nil)   }  func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) {     let image = info[uiimagepickercontrollereditedimage] as! uiimage      avatarimageview.image = image      picker.dismiss(animated: true, completion: nil)    }  func imagepickercontrollerdidcancel(_ picker: uiimagepickercontroller) {     picker.dismiss(animated: true, completion: nil) } 

since app going out of memory, you'll need kind of persistence mechanism saving image. simplest way store image in userdefaults. can accomplished this:

func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) {      let image = info[uiimagepickercontrollereditedimage] as! uiimage     avatarimageview.image = image     userdefaults.standard.set(uiimagepngrepresentation(image), forkey: "avatarimage")          picker.dismiss(animated: true, completion: nil) } 

then when reopen app you'll need check whether you've saved avatarimage in userdefaults , load there:

// in viewdidload or wherever else load image override func viewdidload() {      if let imagedata = userdefaults.standard.object(forkey: "avatarimage") as? data {         avatarimageview.image = uiimage(data: imagedata)     } } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -