【问题标题】:Swift displayed firebase photoUrl cannot replace image by image pickerSwift显示的firebase photoUrl无法通过图像选择器替换图像
【发布时间】:2016-07-18 00:13:00
【问题描述】:

我使用 Firebase 作为我的应用程序的后端。当用户完成身份验证后,他们将转到配置文件创建页面。它显示来自 facebook 的用户个人资料图片。

我用这段代码来显示

func displayProfilePic(user: FIRUser?){

let photoURL = user?.photoURL
struct last {
  static var photoURL: NSURL? = nil
}
last.photoURL = photoURL;  // to prevent earlier image overwrites later one.
if let photoURL = photoURL {
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
    let data = NSData.init(contentsOfURL: photoURL)
    if let data = data {
      let image = UIImage.init(data: data)
      dispatch_async(dispatch_get_main_queue(), {
        if (photoURL == last.photoURL) {
          self.profilePic.image = image
        }
      })
    }
  })
} else {
  profilePic.image = UIImage.init(named: "DefaultPic")
}

不过,我也让用户通过相机或照片库选择自己的头像。当用户选择他们的图像时,它将显示选择的图像 1 到 2 秒,并显示回 facebook 个人资料图片。 也就是说,“挑选出来的图片不能代替脸书头像”

这是我的相机和照片库代码

func openGallary(){

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
  print("Pick Photo")

  self.imagePicker.delegate = self
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
  self.imagePicker.allowsEditing = true
  self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
}

func openCamera(){
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
  self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
  self.imagePicker.allowsEditing = true
  self.presentViewController(self.imagePicker, animated: true, completion: nil)

}else{
  print("you got no camara")
}
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: { () -> Void in

})
profilePic.image = image
}

【问题讨论】:

    标签: swift firebase firebase-authentication


    【解决方案1】:

    Firebase 身份验证会拍摄照片URL,即用户个人资料照片所在的公共位置。如果您想允许用户上传新的个人资料照片,则必须找到存储照片的位置,然后将 URL 放入 Firebase 身份验证配置文件中。

    保存此类个人资料图片的一个地方是 Firebase 存储。请参阅 how to upload an image from iOSgenerate a download URL 上的文档,您存储在用户配置文件中。

    【讨论】:

    • 嗨@Frank,我明白你的意思,但我的问题是当“photoUrl”图像存在时,图像视图无法替换/显示新图像。我认为每次用户选择个人资料图片时都不需要更换“photoUrl”。如果我没有把我的问题说清楚,我很抱歉。
    • 或者我是否需要在每次用户选择新照片时使用“changeRequest”来更改 photoUrl。在此之前,我认为当用户按下上传按钮(上传到 Firebase)时我可以完成所有提交
    猜你喜欢
    • 2022-07-13
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2018-05-23
    • 2017-03-30
    • 2023-01-04
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多