【问题标题】:Save profile picture associated with the user on Firebase在 Firebase 上保存与用户关联的个人资料图片
【发布时间】:2018-04-12 09:21:14
【问题描述】:

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

【问题讨论】:

  • 如果您想为当前用户保存个人资料图片,Firebase 支持。有关详细信息,请参阅我的答案。如果它只是存储图像,我的回答中有一个链接到该任务的 Firebase 存储。

标签: ios image firebase photo firebase-storage


【解决方案1】:
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 存储的 .putData 函数将图像数据上传到 Firebase 存储。您可以捕获任务引用(即uploadTask)以观察上传进度。

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

【讨论】:

  • profileImgReference.downloadURL { url, error in if let error = error { // Handle any errors } else { // Get the download URL } }
【解决方案2】:

您必须使用Firebase Storage 上传图片,然后获取 URL 并将 URL 保存在数据库中的某个位置。

这里是关于如何上传文件的文档:https://firebase.google.com/docs/storage/ios/upload-files

这是我的一个项目中的一个例子

FIRStorageReference *ref = [[[FIRStorage storage] reference] child:[NSString stringWithFormat:@"images/users/profilesPictures/pp%@.jpg", [UsersDatabase currentUserID]]];
[ref putData:imageData metadata:nil completion:^(FIRStorageMetadata * _Nullable metadata, NSError * _Nullable error) {
    if (error) {
        [[self viewController] hideFullscreenLoading];
        [[self viewController] showError:error];
    } else {
        [ref downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
            if (error) {
                [[self viewController] hideFullscreenLoading];
                [[self viewController] showError:error];
            } else {
                [[self viewController] hideFullscreenLoading];
                [self.profilePictureButton setImage:nil forState:UIControlStateNormal];
                [[UsersDatabase sharedInstance].currentUser setProfilePictureURL:[URL absoluteString]];
                [UsersDatabase saveCurrentUser]; // This also updates the user's data in the realtime database.
            }
        }];
    }
}];

【讨论】:

    【解决方案3】:

    幸运的是,这是一个超级简单的任务。

    Firebase 用户对象有一个 photoUrl 属性,可以像这样读取

    let user = Auth.auth().currentUser
    if let user = user {
      let uid = user.uid
      let email = user.email
      let photoURL = user.photoURL
      // ...
    }
    

    网址可以是 Firebase 外部的网址或保存在 Firebase storage 中的网址

    更改照片网址:

    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
    changeRequest?.displayName = //if you want to change the displayName
    changeRequest?.photoURL = //some Url
    changeRequest?.commitChanges { (error) in
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 2017-05-04
      • 1970-01-01
      • 2020-01-23
      • 2018-07-16
      • 2023-03-28
      • 2015-05-22
      • 1970-01-01
      相关资源
      最近更新 更多