如何通过API URL显示图像?迅速 [英] How can I display an image by an API URL? Swift
问题描述
我有一个应用程序,其中向我显示来自API的数据.我做了一个获取数据的服务,其中URL(string =")是API的URL:
I have an app in which it shows me data from an API. I made a service that gets the data, where URL(string = "") is the url of the API:
class Webservice {
func getAllMatches(completion: @escaping ([Matches.Matchs]?) -> ()) {
guard let url = URL(string: "API")
else {
fatalError("URL is not correct!")
}
URLSession.shared.dataTask(with: url) { data, _, _ in
let matchs = try! JSONDecoder().decode([Matches.Matchs].self, from: data!)
DispatchQueue.main.async {
completion(matchs)
}
}.resume()
}
}
然后我创建了一个其中设置了API字段的ViewModel:
Then I made a ViewModel in which the API fields were set:
class MatchListViewModel: ObservableObject {
@Published var matches = [MatchViewModel]()
init() {
fetchMatch()
}
func fetchMatch() {
Webservice().getAllMatches { matches in
if let matches = matches {
self.matches = matches.map(MatchViewModel.init)
}
}
}
}
class MatchViewModel {
let id = UUID()
var match: Matches.Matchs
init(match: (Matches.Matchs)) {
self.match = match
}
var championship: String {
return self.match.championship.name
}
var local_Name: String {
return self.match.local.name
}
var local_Image: String {
return self.match.local.image
}
var local_goals: Double {
return self.match.local_goals
}
var local_penalty_goals: Double {
return self.match.local_penalty_goals
}
var stadium_Name: String {
return self.match.stadium.name
}
}
在API中,有一个显示图像的字段,该字段包含在URL中,我举一个例子:
In the API, there is a field that shows an image, which is contained in a URL, I leave an example:
"local":{
"slug":"nombre",
"name":"nombre",
"short_name":"nombre",
"image":"https://s3.amazonaws.com/funx-futbol/dashboard/ad.png"
},
我的问题是:如何通过该URL显示该图像?API查询有效,因为它为我带来了我所需的信息,但是我需要通过该链接向我展示图像,但是我没有实现.
My question is: How can I display that image via that URL? The API query works, as it brings me the information I require, but I need to show me the image through that link, which I have not achieved.
感谢阅读!
推荐答案
使用这样的加载器加载数据:
Use a loader like this to load the data:
import Combine
public class DataLoader: ObservableObject {
public let objectWillChange = PassthroughSubject<Data,Never>()
public private(set) var data = Data() {
willSet {
objectWillChange.send(newValue)
}
}
private let resourseURL: URL?
public init(resourseURL: URL?){
self.resourseURL = resourseURL
}
public func loadImage() {
guard let url = resourseURL else {
return
}
URLSession.shared.dataTask(with: url) { (data,_,_) in
guard let data = data else {
return
}
DispatchQueue.main.async {
self.data = data
}
} .resume()
}
}
和一个类似的结构来显示它:
And a struct like this to display it:
import SwiftUI
struct WebImage: View {
@ObservedObject private var imageLoader: DataLoader
public init(imageURL: URL?) {
imageLoader = DataLoader(resourseURL: imageURL)
}
public var body: some View {
if let uiImage = UIImage(data: self.imageLoader.data) {
return AnyView(Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: ContentMode.fit))
} else {
return AnyView(Image(systemName: "ellipsis")
.onAppear(perform: { self.imageLoader.loadImage() }))
}
}
}
这篇关于如何通过API URL显示图像?迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!