在上传到firebase之前,如何使用SwiftUI预览图像?
我随后成功地将图像上传到我的Firebase存储中 但问题是,即使你点击一次,它也会上传图片。没有向用户提供确认机会,我认为这是不好的 这是整个结构在上传到firebase之前,如何使用SwiftUI预览图像?,swift,firebase,swiftui,firebase-storage,Swift,Firebase,Swiftui,Firebase Storage,我随后成功地将图像上传到我的Firebase存储中 但问题是,即使你点击一次,它也会上传图片。没有向用户提供确认机会,我认为这是不好的 这是整个结构 struct imagePicker : UIViewControllerRepresentable{ func makeCoordinator() -> imagePicker.Coordinator { return imagePicker.Coordinator(parent1: self) }
struct imagePicker : UIViewControllerRepresentable{
func makeCoordinator() -> imagePicker.Coordinator {
return imagePicker.Coordinator(parent1: self)
}
@Binding var shown : Bool
func makeUIViewController(context: UIViewControllerRepresentableContext<imagePicker>) ->
UIImagePickerController {
let imagepic = UIImagePickerController()
imagepic.sourceType = .photoLibrary
imagepic.delegate = context.coordinator
return imagepic
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<imagePicker>) {
}
class Coordinator : NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var parent : imagePicker!
init(parent1: imagePicker ){
parent = parent1
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
parent.shown.toggle()
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[.originalImage] as! UIImage
let storage = Storage.storage()
let storingimage = storage.reference().child("temp.jpg")
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storingimage.putData(image.jpegData(compressionQuality: 0.35)!, metadata: metadata)
{(response,err) in
if(err != nil){
print(err?.localizedDescription)
}
else{
// You can also access to download URL after upload.
storingimage.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
// upload to
// I plan on storing this URL downloadURL
}
}
}
}
}
}
我不知道如何创建预览,因为在这种情况下,imagePicker
在UIImage
中工作,我认为它不能非常直观地表现为Image
。我学到了一些技巧。基本上,我需要通过添加一个image变量来修改我的makeCoordinator()
struct imagePicker {
@Binding var shown: Bool
@Binding var image: Image?
func makeCoordinator() -> imagePicker.Coordinator {
return imagePicker.Coordinator(parent1: self, image: $image)
}
}
在contentView中,我现在可以读取返回的值
我的VStack
if self.image != nil{
self.image?.resizable()
.frame(width: 250)
.shadow(radius: 10)
}
if(self.showCaptureImageView){
imagePicker(shown: self.$showCaptureImageView, image: self.$image)
}
在结构的正下方
@State var showCaptureImageView = false
@State var image: Image? = nil
现在,用户可以在预览中看到他们选择的照片。非常直观地
图像(uiImage:\u您的图像\u此处)
。例如,请参见@Asperi yes,我在func makeCoordinator()->imagePicker.Coordinator中缺少image
参数。我要写一个答案
@State var showCaptureImageView = false
@State var image: Image? = nil