0

I am attempting to download/display a JPEG image from a URL path saved in my MYSQL DB. I can retrieve the path in full and search for it on a browser which verifying it exists within the DB. However, I once I have the URL path, I can not download it properly or display it as a UIImage in a cell on in my TableView Controller.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "activity", for: indexPath) as! CustomCell

    let post = activities[indexPath.row]

    //print(post["path"])


    //let image = images[indexPath.row]
    let imaged = post["path"] as! String
    //print(imaged)

    let image = URL(string: imaged)! //download url
    print(image)

    let username = post["username"] as? String
    let text = post["text"] as? String

    let session = URLSession(configuration: .default)
    let downloadPicTask = session.dataTask(with: image) {
        (data, response, error) in
        if let e = error {
            print("Error downloading image: \(e)")
        } else {
            if (response as? HTTPURLResponse) != nil {
                if let imageData = data {
                    let pic = UIImage(data: imageData)
                    print(pic)
                    cell.activityImage.image = pic
                    self.images.append(pic!)
                } else{
                    print("couldn't get image: image is nil")
                }
            } else {
                print("Couldn't get response code")
            }

        }
    }
    cell.titleLabel.text = text
    cell.usernameLabel.text = username


    downloadPicTask.resume()
    return cell
}
schaboi24
  • 11
  • 1

0 Answers0