将图像上传到Firebase存储中

问题描述 投票:0回答:1

我曾尝试将用户图像上传到Firebase存储,但是一旦上传,图像将显示为空白,我该怎么做才能修复此代码。

 let storage = Storage.storage()
    let storageRef = Storage.storage().reference(forURL: "firebase.appspot.com").child("profile_Image")
    if let ProfileImge = self.SelectedImage, let imagedata = ProfileImge.jpegData(compressionQuality: 0.1) {
        storageRef.putData(imagedata, metadata: nil) { (metadata, error) in
            let ProfilePic = storageRef.child("Profile_Image.jpg")
            let metadata = StorageMetadata()
            metadata.contentType = "image/jpeg"

            ProfilePic.downloadURL { url, error in
                if let error = error {
                    // Handle any errors
                } else {
                    // Get the download URL for 'images/stars.jpg'
                }
                return
            }
        }
    }
ios download return firebase-storage
1个回答
0
投票

尝试

let imageName = "image1.jpg"
let image = UIImage(named: imageName)    
let store = Storage.storage()        
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let imageData: Data = UIImageJPEGRepresentation(image, 0.5)!
let storeRef = store.reference().child("Images")
let _ = storeRef.putData(imageData, metadata: metadata) { (metadata, error) in
    guard let _ = metadata else {
        print("error occurred: \(error.debugDescription)")
        return
    }
    let pat = (metadata?.downloadURL()?.absoluteString.description)
    let link = pat! //Link of image

}
© www.soinside.com 2019 - 2024. All rights reserved.