Search code examples
iosswiftsdwebimage

Why I can't download png format file using SDWebImage?


I am trying to debug my code, and I find out that the problem why my image doesn't appear because somehow I can't download my png format file using SDWebImage.

if the format is jpg it can work properly. here is the full simplified code in the table view controller.

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var imageURLs = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        imageURLs = ["https://app10.pakubuwono6.com/hris/images/notification/Logo Small.png", "https://app10.pakubuwono6.com/hris/images/notification/Logo Small.png", "https://app10.pakubuwono6.com/hris/images/notification/Logo Small.png", "https://app10.pakubuwono6.com/hris/images/notification/Logo Small.png"]
        tableView.reloadData()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return imageURLs.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellImage") as! CellImage
        cell.imageExample.sd_setImage(with: URL(string: imageURLs[indexPath.row]), placeholderImage: UIImage(named: "placeholder"), options: [.continueInBackground, .progressiveDownload])
        return cell
    }
}

I am using the line of code below to download the image, I set the image in the table view cell in this view controller

cell.imageExample.sd_setImage(with: URL(string: imageURLs[indexPath.row]), placeholderImage: UIImage(named: "placeholder"), options: [.continueInBackground, .progressiveDownload])

what went wrong in here?

just appear the placeholder image enter image description here


Solution

  • Your image url include white space try this code

    if let url = imageURLs[indexPath.row].addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed){
     cell.imageExample.sd_setImage(with: URL(string: url), placeholderImage: UIImage(named: "placeholder"), options: [.continueInBackground, .progressiveDownload])
    
    }