2016-07-18 5 views
0

Ich verwende Firebase als Back-End meiner App. Wenn der Benutzer die Authentifizierung beendet hat, wird er zur Profilerstellungsseite weitergeleitet. Es zeigt das Benutzerprofilbild von Facebook an.Swift angezeigte Firebase PhotoUrl kann Bild-für-Bild-Picker nicht ersetzen

Ich benutze diesen Code

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") 
} 

jedoch, angezeigt werden auch ich lasse Benutzer ihre eigenen Profilbild Bibliothek von Kamera oder Foto wählen. Wenn der Benutzer sein Bild auswählt, wird das ausgewählte Bild für 1 bis 2 Sekunden angezeigt und das Facebook-Profilbild wird angezeigt. das bedeutet ist, das

Es ist mein Code für die Kamera und Foto-Bibliothek

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 
} 

Antwort

0

Firebase Authentifizierung nimmt ein Foto URL, so die öffentliche Stelle „abgreifbar Bild das Facebook-Profilbild nicht ersetzen“ wo ein Profilfoto für den Benutzer vorhanden ist. Wenn Sie dem Benutzer das Hochladen eines neuen Profilfotos erlauben möchten, müssen Sie einen Speicherort für das Foto suchen und die URL dann in das Firebase-Authentifizierungsprofil einfügen.

Ein Platz zum Speichern eines solchen Profilbilds wäre Firebase Storage. Siehe die Dokumentation zu how to upload an image from iOS und dann generate a download URL, die Sie im Benutzerprofil speichern.

+0

Hallo @Frank, ich verstehe, was du meinst, aber mein Problem ist die Imageview kann ein neues Bild nicht ersetzen/anzeigen, wenn das "FotoUrl" Bild dort ist. Ich glaube nicht, dass ich das "photoUrl" jedes Mal ersetzen muss, wenn der Benutzer sein Profilbild ausgewählt hat. Es tut mir leid, wenn ich meine Frage nicht klargestellt habe. –

+0

oder muss ich "changeRequest" verwenden, um die photoUrl jedes Mal zu ändern, wenn der Benutzer ein neues Foto ausgewählt hat. Davor dachte ich, ich könnte alle Einsendungen machen, wenn der Benutzer den Upload Button gedrückt hat (Upload in Firebase) –