ios - UIImagePickerController presented from UIImageView inside UICollectionViewCell -


i having trouble loading selected image uiimagepickercontroller inside uiimageview. uiimageview presented on tap of button inside collectionviewcell. on tap of button inside collectionviewcell uiimageview animates view. on tap of uiimageview uiimagepicker presented. picker dismisses fine when image selected, image not load inside uiimageview on picker dismiss (the placeholder image remains , not replaced selected image). no crashes or errors occurring in console. help!

uicollectionviewcell class - logincell

class logincell: uicollectionviewcell, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate {  lazy var createusernamebutton: uibutton = {     let custombutton = uibutton(type: .system)     custombutton.settitle("create username", for: .normal)     custombutton.addtarget(self, action: #selector(animatein), for: .touchupinside)     return custombutton }()  func animatein() {     addsubview(profileimageview)      profileimageview.transform = cgaffinetransform.init(scalex: 1.3, y: 1.3)     profileimageview.alpha = 0      uiview.animate(withduration: 0.2) {         self.profileimageview.alpha = 1         self.profileimageview.transform = cgaffinetransform.identity     }  }      lazy var profileimageview: uiimageview = {         let imageview = uiimageview()         imageview.image = uiimage(named: "avatar-bg-2x")         imageview.addgesturerecognizer(uitapgesturerecognizer(target:self, action: #selector(handleselectprofileimage)))         imageview.isuserinteractionenabled = true         return imageview     }()      var logincontroller: logincontroller?      func handleselectprofileimage() {         guard let logincontroller = delegate as? logincontroller else {             return         }         logincontroller.showimagepicker()     }  //..... } 

logincontroller class

class logincontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource, uicollectionviewdelegateflowlayout, logincontrollerdelegate, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate {      weak var logincollectionview: uicollectionview!     var logincell: logincell?      func showimagepicker() {         let picker = uiimagepickercontroller()         picker.delegate = self         picker.allowsediting = true         present(picker, animated: true, completion: nil)     }      func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) {          var selectedimagefromimagepicker: uiimage?          if let editedimage = info["uiimagepickercontrollereditedimage"] as? uiimage {             selectedimagefromimagepicker = editedimage         } else if let originalimage = info["uiimagepickercontrolleroriginalimage"] as? uiimage {             selectedimagefromimagepicker = originalimage         }          dismiss(animated: true, completion: {             if let selectedimage = selectedimagefromimagepicker {                 self.logincell?.profileimageview.image = selectedimage                 print("was dismissed")             }         })     }      func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {             let logincell = collectionview.dequeuereusablecell(withreuseidentifier: logincellid, for: indexpath) as! logincell             logincell.delegate = self             return logincell     } // ... } 

i found problem, not updating image on main thread. try this:

1. change code in login controller class match code below.

func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) {      var selectedimagefromimagepicker: uiimage?      if let editedimage = info["uiimagepickercontrollereditedimage"] as? uiimage {         selectedimagefromimagepicker = editedimage     } else if let originalimage = info["uiimagepickercontrolleroriginalimage"] as? uiimage {         selectedimagefromimagepicker = originalimage     }      if let selectedimage = selectedimagefromimagepicker {         dispatchqueue.main.async {             self.logincell?.profileimageview.image = selectedimage             self.logincollectionview.reloaddata() //edit: add new piece of code         }     }      dismiss(animated: true, completion: nil) }  var profileimage: uiimage?  func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         let logincell = collectionview.dequeuereusablecell(withreuseidentifier: logincellid, for: indexpath) as! logincell         logincell.delegate = self         logincell.profileimageview.image = profileimage != nil ? profileimage : uiimage(named: "avatar-bg-2x")         return logincell } 

2. change code in logincell class match code below.

lazy var profileimageview: uiimageview = {     let imageview = uiimageview()     //removed line here     imageview.addgesturerecognizer(uitapgesturerecognizer(target:self, action: #selector(handleselectprofileimage)))     imageview.isuserinteractionenabled = true     return imageview }() 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -