【问题标题】:unable to get image from URL to update in UIImage in swift无法从 URL 获取图像以在 UIImage 中快速更新
【发布时间】: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)
>                                        }
>             
>            }

【问题讨论】:

标签: ios json swift uitableview asynchronous


【解决方案1】:

我尝试按照以下参考链接中的说明安装 SVGKit,它对我有用。 How to display .svg image using swift

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
            if let url = NSURL(string: "https://restcountries.eu/data/est.svg"){
                    if let data = NSData(contentsOf: url as URL) {
                        let imageAux = UIImage(data: data as Data)

                        DispatchQueue.main.async {
                    let anSVGImage: SVGKImage = SVGKImage(data: data as Data)
                                cell.imgFlag.image = nSVGImage.uiImage
                        }
                    }
                }
        }

在您现有的代码中只需添加一行代码: 让 anSVGImage: SVGKImage = SVGKImage(data: data as Data) 它在我的最后工作。

【讨论】:

    【解决方案2】:

    我尝试使用WKWebView,对我来说,下面的代码正在运行:

    if let svgString =
      try ? String(contentsOf: URL(string: "https://myantion.eu/data/sol.svg") !) {
        self.wkWebView.loadHTMLString(svgString, baseURL: URL(string: "https://myantion.eu/data/sol.svg") !)
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多