Skip to content

Displaying a Firestore image in SwiftUI

   

How to display a Firestore image in SwiftUI. UIImage can be easily displayed using SDWebImage, but SwiftUI can't do that. I used SDWebImage/SDWebImageSwiftUI to write the following I feel like there's a better way to write this, so that it's cached, including the tokens.

Reference: iOS でファイルをダウンロードする SDWebImage/SDWebImageSwiftUI

import SwiftUI
import UIKit
import SDWebImageSwiftUI
import FirebaseStorage
struct ProfileCell: View {
let profile: Profile
@State var urlString: String = ""
var body: some View {
ZStack {
WebImage(url: URL(string: self.urlString))
.resizable()
.placeholder {
Image("profile")
.resizable()
}
.animation(.easeInOut(duration: 0.5))
.transition(.fade)
.scaledToFill()
VStack {
Spacer()
Text(profile.name)
.font(Font.system(size: 20.0).bold())
.frame(maxWidth: .infinity, maxHeight: 32)
.background(Color.gray)
.foregroundColor(Color.white)
.opacity(0.8)
}
}.onAppear {
self.featchImageUrl()
}
}
func featchImageUrl() {
let storage = Storage.storage()
let pathReference = storage.reference(withPath: "character/\(profile.id)/profile.png")
pathReference.downloadURL { url, error in
if let error = error {
// Handle any errors
print(error)
} else {
guard let urlString = url?.absoluteString else {
return
}
self.urlString = urlString
}
}
}
}

  1. Detecting changes to the SwiftUI Toggle
  2. Creating a multi-line picker in SwiftUI
  3. Don't show the Label in the SwiftUI Picker
  4. Displaying a Picker in SwiftUI
  5. Calling the transition source method from the view of the screen transition destination in SwiftUI
  6. Create a sequential numbered array in Swift
  7. Using TabView to switch screens (SwiftUI)