【发布时间】:2019-02-04 05:57:06
【问题描述】:
我正在尝试使用来自 web api 的图像更新 tableview 单元格。 根据下面的链接,我试过了,但即使图像存在,它也会在图像中给出 nil。
Update table cell image asynchronously.
下面是我的代码,请让我知道我错过了它。
DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
if let url = NSURL(string: "https://myantion.eu/data/sol.svg"){
if let data = NSData(contentsOf: url as URL) {
let imageAux = UIImage(data: data as Data)
DispatchQueue.main.async {
cell.imgFlag.image = imageAux
}
}
}
}
即使图像存在,imageAux 也会给我 nil 值
根据以下建议,即使尝试使用 UIWebView 仍然没有结果,下面是代码:
> let fileURL:URL = URL(fileURLWithPath:
> "https://myantion.eu/data/sol.svg")
> let req = URLRequest(url: fileURL)
> DispatchQueue.main.async {
> cell.webVw.loadRequest(req)
> }
>
> }
【问题讨论】:
-
这会将我带到一个带有图像的网站,但似乎不是图像本身的
URL。 -
参见stackoverflow.com/questions/35691839/… 看起来 UIImage 初始化程序采用 png 或 jpeg,但不是 SVG。您需要执行某种转换或使用 webview
-
URL 中的图片类型是 .svg 而不是 UIImage。因此,最好先将 .svg 转换为 .jpg 或 png。 stackoverflow.com/questions/32808901/svg-url-to-uiimage-ios
-
github.com/mchoe/SwiftSVG 使用这个插件。
标签: ios json swift uitableview asynchronous