import SwiftUI
extension String{
func load() ->UIImage {
do{
//string to URL
guard let url = URL(string: self) else{
//empty image if wrong url
return UIImage()
}
//url to data
let data : Data = try Data(contentsOf: url)
//create uuimage obj from data
return UIImage(data: data) ?? UIImage()
}catch{
}
return UIImage()
}
}
struct ContentView: View {
@State private var quoteData : QuoteData?
var body: some View {
ZStack{
VStack{
Text(quoteData?.lat ?? "Latitude")
Text(quoteData?.long ?? "Longitude")
//------------------------HELP HERE
Image(uiImage: "https://maps.googleapis.com/maps/api/staticmap?center=LATITUDE,LONGITUDE&zoom=16&size=400x400&key=AIzaSyClGOTMpV2kKF27bxAo6nm3pIq7zmW69Fw".load())
struct QuoteData: Decodable{
//var id: Int
var long: String
var lat: String
var apiURL: String
}
I want to insert the long and lat variables of QuoteData inside the url string that is used in IMAGE(uimage
I cant find an answer , been looking for hours with no luck
this is just a fragment of the code it will be nice if someone can help me .
CodePudding user response:
try something like this, using \(lat),\(lon)
"...to insert the long and lat variables of QuoteData inside the url string...":
struct ContentView: View {
@State private var quoteData: QuoteData? = QuoteData(long: "139.7514", lat: "35.685", apiURL: "") // for testing, Tokyo
var body: some View {
VStack{
Text(quoteData?.lat ?? "Latitude")
Text(quoteData?.long ?? "Longitude")
if let lat = quoteData?.lat, let lon = quoteData?.long {
Image(uiImage: "https://maps.googleapis.com/maps/api/staticmap?center=\(lat),\(lon)&zoom=16&size=400x400&key=AIzaSyClGOTMpV2kKF27bxAo6nm3pIq7zmW69Fw".load())
}
}
}
}
PS: this has nothing to do with JSON object to String