0

So the way I struct my retrieved data is by;

struct framePic { let imagedB : Data! let pricedB : String! }

I've managed to retrieve the data but I dont know how to set image on tableView? I've managed to figure it out with a label... but not with image?

this is the code for the cell in the table;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView

     // sets label
    cell.priceView.text = framePics[indexPath.row].pricedB

    // set image on UIimageView???
    --- I dont know how to add this? I need somehow to get the list of only the images???  I believe if I had the array variable like; imageFiles[] I could have done;
imageFiles[indexPath.row].getDataInBackground... But in my case I use struct so how am I supposed to do this in this case?

the RowCellTableView just contains;

  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var priceView: UILabel!
Adam A.
  • 125
  • 15
StrawHat
  • 351
  • 1
  • 6
  • 14

2 Answers2

0

In your struct you have:

struct framePic { 
   let imagedB : Data! 
   let pricedB : String! 
}

Try to convert the imagedB to UIImage like this:

var imageUIImage: UIImage = UIImage(data: imageData)

So your implementation would be like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView

     // sets label
    cell.priceView.text = framePics[indexPath.row].pricedB

    // convert imageData to UIImage
    var imageUIImage: UIImage = UIImage(data: framePics[indexPath.row].imagedB)

    // apply image to UIImageView
    cell.imageView.image = imageUIImage

    return cell
}
Rafael Flecha
  • 324
  • 4
  • 6
-1
cell.imageView.image = someImage
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
temp
  • 639
  • 1
  • 8
  • 22
  • Actually since you are fetching this data from Firebase, you aren't necessarily pulling the image, your pulling the URL to the image. If you use a framework that allows you pass in a URL to an imageView you could do that instead and only pass in the URL – temp Aug 11 '17 at 20:56
  • Yes I fetch a URL, so what kind of framework support this? to enter the URL and fetch the image?? hm... – StrawHat Aug 11 '17 at 21:09
  • @StrawHat this will definetly be of use to you. Its a great list of iOS libraries you can easily integrate into your project via Cocoapods. https://github.com/vsouza/awesome-ios#image – temp Aug 11 '17 at 21:11
  • @StrawHat I would reccommend SDWebImage, however look into which one suites you best – temp Aug 11 '17 at 21:12
  • Could you tell me one specifically? I both save a URL in the database and save the image in storage... – StrawHat Aug 11 '17 at 21:12
  • https://stackoverflow.com/questions/41132122/how-to-download-image-with-url-swift-3 this question may better help you understand. Pls mark as answered if this helps :) – temp Aug 11 '17 at 21:15
  • Im not really a fan of just using frameworks just to use them, I mean since I use FireBase there is probably a way of handling this... I might just have to wait for another answer.. I will try to check your link out... – StrawHat Aug 11 '17 at 21:17
  • Sorry I realize the link I gave you linked to another answer. That answer actually provides you ways of doing it without using external frameworks via an extension on UIImageView. That is probably what you are looking for. – temp Aug 11 '17 at 21:18
  • Review comment: Your answer, while maybe correct, contains **no** explanation. Worse, several things you've said in the comments to your answer **belong** in the answer! –  Aug 11 '17 at 22:02
  • Please add some explanation to your answer. – Emre Önder May 07 '18 at 10:41