Uploading an Image instead of an UIImage to Firestore

这一生的挚爱 提交于 2021-01-16 04:14:56

问题


Using Swift 5.0 / SwiftUI 2.0

One method available when uploading an UIImage to Firestore is jpegData(compressionQuality: Double) which essentially makes the whole process of fetching the saved image a lot faster.

Is there any equivalent for Image yet or any other viable workarounds or its as simple as either UIImage or poor UX


回答1:


struct ContentView: View {
    
    //Here
    @State var uiImage: UIImage?
    
    var body: some View {
        VStack {
            if uiImage != nil {
                Image(uiImage: uiImage!)
            }
            
            
            Button(action: {
                downloadImage()
            }, label: {
                Text("Download image")
            })
        }
        
    }

    func downloadImage() {
        URLSession.shared.dataTask(with:URL(string: "https://upload.wikimedia.org/wikipedia/commons/3/3f/Self-portrait_taken_with_laptop%2C_March%2C_2018.jpg")!) { (data, response, eooror) in
            guard data != nil else { return }
            
            DispatchQueue.main.async {
                self.uiImage = UIImage(data: data!)
            }
    
        }.resume()
    }
    
    //Here
    func getData() {
        var data = uiImage?.jpegData(compressionQuality: 0.5)
    }
}


来源:https://stackoverflow.com/questions/65614931/uploading-an-image-instead-of-an-uiimage-to-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!