如何从watchos 2上的URL加载图像?

时间:2016-11-22 10:16:03

标签: watchkit apple-watch watch-os-2

是否可以使用任何框架从watchos 2中的URL加载图像?我试过SDWebImage,但它不支持watchos2?

1 个答案:

答案 0 :(得分:0)

它没有框架,但我写了这个扩展名:

extension WKInterfaceImage {
    public func imageFromUrl(_ urlString: String) {

        if let url = NSURL(string: urlString) {

            let request = NSURLRequest(url: url as URL)
            let config = URLSessionConfiguration.default
            let session = URLSession(configuration: config)

            let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
                if let imageData = data as Data? {
                    DispatchQueue.main.async {
                        self.setImageData(imageData)
                    }
                }
            });

            task.resume()
        }
    }
}

只需将wKInterfaceImage添加到watchOS故事板,附加它然后调用即可 imageView.imageFromUrl("https://something")

让我知道它是否有效!