我使用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图像。 这意味着,"选择的图片无法取代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
}
答案 0 :(得分:0)
Firebase身份验证会拍摄照片网址,因此会显示用户个人资料照片的公共位置。如果您希望允许用户上传新的个人资料照片,则必须找到存储照片的位置,然后将该网址放入Firebase身份验证配置文件中。
保留此类个人资料照片的一个地方是Firebase存储。请参阅how to upload an image from iOS上的文档,然后查看存储在用户个人资料中的generate a download URL。