为什么我不能使用 SDWebImage 下载 png 格式的文件?
Why I can't download png format file using SDWebImage?
我正在尝试调试我的代码,我发现我的图像没有出现的问题是因为我无法使用 SDWebImage 下载我的 png 格式文件。
如果格式为jpg,则可以正常使用。这是 table 视图控制器中的完整简化代码。
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
}
}
我正在使用下面的代码行下载图像,我在这个视图控制器的 table 视图单元格中设置了图像
cell.imageExample.sd_setImage(with: URL(string: imageURLs[indexPath.row]), placeholderImage: UIImage(named: "placeholder"), options: [.continueInBackground, .progressiveDownload])
这里出了什么问题?
只显示占位符图像
您没有显示用于加载图像的 setImage(with:placeholderImage:options:)
方法,因此很难判断到底发生了什么。
不过,一个明显的问题是您的图像 URLS 包含 un-escaped 空格。您应该像这样更改 URL:
imageURLs =
["https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png"]
(请注意,您不能像其他答案中建议的那样在整个 URL 字符串上使用 addingPercentEncoding
,因为不应转义 URL 字符串中的其他符号,只是文件名。)
使用 URLComponents
构建 URL 真的更好。你指定 URL 的不同部分,然后要求 URLComponents
给你一个 URL。它为您正确地转义了不同的组件。
你的图片url包含白色space
试试这个代码
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])
}
我正在尝试调试我的代码,我发现我的图像没有出现的问题是因为我无法使用 SDWebImage 下载我的 png 格式文件。
如果格式为jpg,则可以正常使用。这是 table 视图控制器中的完整简化代码。
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
}
}
我正在使用下面的代码行下载图像,我在这个视图控制器的 table 视图单元格中设置了图像
cell.imageExample.sd_setImage(with: URL(string: imageURLs[indexPath.row]), placeholderImage: UIImage(named: "placeholder"), options: [.continueInBackground, .progressiveDownload])
这里出了什么问题?
只显示占位符图像
您没有显示用于加载图像的 setImage(with:placeholderImage:options:)
方法,因此很难判断到底发生了什么。
不过,一个明显的问题是您的图像 URLS 包含 un-escaped 空格。您应该像这样更改 URL:
imageURLs =
["https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png",
"https://app10.pakubuwono6.com/hris/images/notification/Logo%20Small.png"]
(请注意,您不能像其他答案中建议的那样在整个 URL 字符串上使用 addingPercentEncoding
,因为不应转义 URL 字符串中的其他符号,只是文件名。)
使用 URLComponents
构建 URL 真的更好。你指定 URL 的不同部分,然后要求 URLComponents
给你一个 URL。它为您正确地转义了不同的组件。
你的图片url包含白色space 试试这个代码
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])
}