将图片从设备上传到Firebase

时间:2019-02-07 20:04:58

标签: ios swift firebase uiimageview firebase-storage

我一直试图将图像上传到Firebase,然后将其设置为用户的个人资料图片。由于某种原因,图片的url地址为null,我相信它与该函数中的元数据有关:

func uploadProfileImage(_ image:UIImage, completion: @escaping((_url:URL?)->()))
{

    guard let uid = Auth.auth().currentUser?.uid else { return }

    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = image.jpegData(compressionQuality: 0.5) else {return}


    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
        }
    }
}

完全返回的url为nil或错误!= nil,metaData == nil

2 个答案:

答案 0 :(得分:1)

以下是将代码添加到Firebase存储和数据库中以供以后使用Swift 4使用的工作代码

func uploadProfileImage(){

    let storedImage = storagebaseRef.child("yourLocation")

    if let uploadData = UIImageJPEGRepresentation(yourImg, 1) 
    {
        storedImage.putData(uploadData, metadata: nil, completion: { (metadata, error) in
            if error != nil {

            }
            storedImage.downloadURL(completion: { (url, error) in
                if error != nil {
                }

                if let urlText = url?.absoluteString {                      
                        self.databaseRef.child("yourLocation").updateChildValues(["pic" : urlText], withCompletionBlock: { (error, ref) in
                           if error != nil {                        
                               self.showAlert(message: "Please Try Again", title: "Error")
                               return
                           }
                        })
                }           
            })       
        })
    }  
}

答案 1 :(得分:0)

这是一种快速解决方案,可以上传具有给定文件名的图像,然后获取URL并将其存储在此用户个人资料中。

目前尚不清楚问题中元数据的用途是什么。

这是未经测试的,所以可能有错字。

func setUsersPhotoURL(withImage: UIImage, andFileName: String) {
    guard let imageData = withImage.jpegData(compressionQuality: 0.5) else { return }
    let storageRef = Storage.storage().reference()
    let thisUserPhotoStorageRef = storageRef.child("this users uid").child(andFileName)

    let uploadTask = thisUserPhotoStorageRef.putData(imageData, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            print("error while uploading")
            return
        }

        thisUserPhotoStorageRef.downloadURL { (url, error) in
            print(metadata.size) // Metadata contains file metadata such as size, content-type.
            thisUserPhotoStorageRef.downloadURL { (url, error) in
                guard let downloadURL = url else {
                    print("an error occured after uploading and then getting the URL")
                    return
                }

                let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
                changeRequest?.photoURL = downloadURL
                changeRequest?.commitChanges { (error) in
                    //handle error
                }
            }
        }
    }
}