OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Firebase 上保存与用户关联的个人资料图片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:00
标题: ios - 在 Firebase 上保存与用户关联的个人资料图片

我正在使用适用于 iOS 的 Firebase 在我的应用程序上,用户必须将照片与他的个人资料相关联 在 MySQL 上有 BLOB 类型来保存数据库中的图像 但是在 Firebase 上我找不到这样的东西



Best Answer-推荐答案


guard let uid = Auth.auth().currentUser?.uid else {return}
guard let imageData = UIImageJPEGRepresentation(profilePic, 0.5) else {return}
        let profileImgReference = Storage.storage().reference().child("profile_image_urls").child("\(uid).png")
        let uploadTask = profileImgReference.putData(imageData, metadata: nil) { (metadata, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                let downloadURL = metadata?.downloadURL()?.absoluteString ?? ""
                // Here you get the download url of the profile picture.
            }
        }
        uploadTask.observe(.progress, handler: { (snapshot) in
            print(snapshot.progress?.fractionCompleted ?? "")
            // Here you can get the progress of the upload process.
        })

第 1 步:使用 UIImageJPEGRepresentation(UIImage, compressionQuality)UIImagePNGRepresentation(UIImage)

将 UIImage 转换为 Jpeg 数据或 PNG 数据

第 2 步:创建存储引用。在上面的示例中,我使用了 Storage.storage().reference() 来获取当前 Firebase 应用程序的存储引用,然后我使用 .child("FolderName ")

第 3 步:使用 Firebase Storage 的 .putData 函数将图像数据上传到 Firebase 存储。您可以捕获任务引用(即 uploadTask)以观察上传进度。

//注意:我使用 Firebase 用户 ID 作为图像名称,因为每个用户 ID 对于 firebase Auth 都是唯一的,并且不会出现不匹配的个人资料图片替换或意外删除。

关于ios - 在 Firebase 上保存与用户关联的个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47031464/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4