Swift - Retrieving Image from Core Data -


i've searched on internet (mostly here) issue , i've tried them still can't work. i'm trying retrieve image core data , display on table view. basically, table view thumbnail image along name , switch. have following swift files below. can lead me do? aside image retrieving, i'm planning add switch when turned on/off asks password can switched on/off. how can start this? hoping kind responses. thank you!

i have snippet of code saving record including image core data.

let moc = (uiapplication.sharedapplication().delegate     as! appdelegate).managedobjectcontext  @iboutlet weak var firstname: uitextfield! @iboutlet weak var lastname: uitextfield! @iboutlet weak var status: uilabel!  @ibaction func saverecord(sender: anyobject) {     let entitydescription =     nsentitydescription.entityforname("faculty",         inmanagedobjectcontext: moc)      let faculty = faculty(entity: entitydescription!,         insertintomanagedobjectcontext: moc)      faculty.firstname = firstname.text     faculty.lastname = lastname.text      let pickedimage = uiimagejpegrepresentation(imageview.image!, 1.0)      faculty.image = pickedimage      {         try moc.save()         firstname.text = ""         lastname.text = ""          status.text = "new record saved."         nsnotificationcenter.defaultcenter().postnotificationname("load", object: nil)         self.dismissviewcontrolleranimated(true, completion: nil)     } catch {         status.text = "record not saved."     }  }  @iboutlet weak var imageview: uiimageview! var newmedia : bool? @ibaction func choosephoto(sender: anyobject) {     if uiimagepickercontroller.issourcetypeavailable(         uiimagepickercontrollersourcetype.savedphotosalbum) {             let imagepicker = uiimagepickercontroller()              imagepicker.delegate = self             imagepicker.sourcetype =                 uiimagepickercontrollersourcetype.photolibrary             imagepicker.mediatypes = [kuttypeimage string]             imagepicker.allowsediting = false             self.presentviewcontroller(imagepicker, animated: true,                 completion: nil)             newmedia = false      }  } 

this tableviewcontroller displays records on table view , supposedly image, 1 i'm trying figure out.

let managedobjectcontext = (uiapplication.sharedapplication().delegate     as! appdelegate).managedobjectcontext  var fetchedlastname = [string]() var fetchedfirstname = [string]()  override func viewdidload() {     super.viewdidload()      nsnotificationcenter.defaultcenter().addobserver(self, selector: "reloaddata:",name:"load", object: nil)      let entitydescription =     nsentitydescription.entityforname("faculty",         inmanagedobjectcontext: managedobjectcontext)      let request = nsfetchrequest()     request.entity = entitydescription      do{         let objects = try managedobjectcontext.executefetchrequest(request)          let results = objects          if results.count > 0 {             var = 0; < results.count; += 1{                 let match = results[i] as! nsmanagedobject                  fetchedlastname.append((match.valueforkey("lastname") as? string)!)                 fetchedfirstname.append((match.valueforkey("firstname") as? string)!)              }         } else {          }     }     catch{} }  func reloaddata(notification: nsnotification){     fetchedlastname.removeall()     fetchedfirstname.removeall()      let entitydescription =     nsentitydescription.entityforname("faculty",         inmanagedobjectcontext: managedobjectcontext)      let request = nsfetchrequest()     request.entity = entitydescription      do{         let objects = try managedobjectcontext.executefetchrequest(request)          let results = objects          if results.count > 0 {             var = 0; < results.count; += 1{                 let match = results[i] as! nsmanagedobject                  fetchedlastname.append((match.valueforkey("lastname") as? string)!)                 fetchedfirstname.append((match.valueforkey("firstname") as? string)!)             }         } else {          }     }     catch{}      self.tableview.reloaddata() } 

am right think somewhere here should write code retrieve image core data , display on table view?

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("teach", forindexpath: indexpath) as! teachtableviewcell      let row = indexpath.row      cell.facultyname.text = fetchedlastname[row] + ", " + fetchedfirstname[row]      return cell } 

ok here fixed code:

teachtableviewcontroller:

// //  teachtableviewcontroller.swift //  teach // //  created icst340.n1 on 9/29/15. //  copyright © 2015 iyotugs. rights reserved. //  import uikit import coredata  class teachtableviewcontroller: uitableviewcontroller { let managedobjectcontext = (uiapplication.sharedapplication().delegate     as! appdelegate).managedobjectcontext  var fetchedlastname = [string]() var fetchedfirstname = [string]() var fetchedimage = [uiimage]()  override func viewdidload() {     super.viewdidload()      nsnotificationcenter.defaultcenter().addobserver(self, selector: "reloaddata:",name:"load", object: nil)      let entitydescription =     nsentitydescription.entityforname("faculty",         inmanagedobjectcontext: managedobjectcontext)      let request = nsfetchrequest()     request.entity = entitydescription      do{         let objects = try managedobjectcontext.executefetchrequest(request)          let results = objects          if results.count > 0 {             var = 0; < results.count; += 1{                 let match = results[i] as! nsmanagedobject                  fetchedlastname.append((match.valueforkey("lastname") as? string)!)                 fetchedfirstname.append((match.valueforkey("firstname") as? string)!)                 //added convert image                 let image = match.valueforkey("image") as! nsdata                 fetchedimage.append(uiimage(data: image)!)              }         } else {          }     }     catch{}     // uncomment following line preserve selection between presentations     // self.clearsselectiononviewwillappear = false      // uncommented     // uncomment following line display edit button in          navigation bar view controller.     //        self.navigationitem.rightbarbuttonitem = self.editbuttonitem() }  func reloaddata(notification: nsnotification){     fetchedlastname.removeall()     fetchedfirstname.removeall()     //added remove images     fetchedimage.removeall()      let entitydescription =     nsentitydescription.entityforname("faculty",         inmanagedobjectcontext: managedobjectcontext)      let request = nsfetchrequest()     request.entity = entitydescription      do{         let objects = try managedobjectcontext.executefetchrequest(request)          let results = objects          if results.count > 0 {             var = 0; < results.count; += 1{                 let match = results[i] as! nsmanagedobject                  fetchedlastname.append((match.valueforkey("lastname") as? string)!)                 fetchedfirstname.append((match.valueforkey("firstname") as? string)!)                 //added convert image                 let image = match.valueforkey("image") as! nsdata                 fetchedimage.append(uiimage(data: image)!)              }         } else {          }     }     catch{}      self.tableview.reloaddata() }   override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  // mark: - table view data source  override func numberofsectionsintableview(tableview: uitableview) -> int {     // #warning incomplete implementation, return number of sections     return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     // #warning incomplete implementation, return number of rows     return fetchedlastname.count }   override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("teach", forindexpath: indexpath) as! teachtableviewcell      let row = indexpath.row      print(fetchedfirstname)      cell.facultyname.text = fetchedlastname[row] + ", " + fetchedfirstname[row]     cell.facultyimage.image = fetchedimage[row]      return cell }  } 

i marked each spot fixed, there 4 spots. 2 of them had taking nsdata , turning image(in viewdidload , reloaddata, same spots). 1 of them add commenting out line made right navbar button edit button. , last 1 fetchedimage.removeall() on top of reloaddata take out of images.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -