如何在 watchos 2 上从 URL 加载图像?
How to load image from URL on watchos 2?
是否有任何框架可用于在 watchos 2 中从 URL 加载图像?我试过 SDWebImage 但它不支持 watchos2?
它没有框架,但我写了这个扩展:
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")
如果有效请告诉我!
是否有任何框架可用于在 watchos 2 中从 URL 加载图像?我试过 SDWebImage 但它不支持 watchos2?
它没有框架,但我写了这个扩展:
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")
如果有效请告诉我!