Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用Facebook个人资料图片作为您的个人资料图片_Swift_Firebase_Firebase Realtime Database_Firebase Authentication_Firebase Storage - Fatal编程技术网

Swift 使用Facebook个人资料图片作为您的个人资料图片

Swift 使用Facebook个人资料图片作为您的个人资料图片,swift,firebase,firebase-realtime-database,firebase-authentication,firebase-storage,Swift,Firebase,Firebase Realtime Database,Firebase Authentication,Firebase Storage,我正在获取facebook的个人资料图片,并将其作为个人资料图片显示在我的应用程序中。这是代码 if let user = FIRAuth.auth()?.currentUser{ let photoUrl = user.photoURL let name = user.displayName self.FacebookUser.text = name let storage = FIRStorage.storage() //refer your p

我正在获取facebook的个人资料图片,并将其作为个人资料图片显示在我的应用程序中。这是代码

if let user = FIRAuth.auth()?.currentUser{
    let photoUrl = user.photoURL
    let name = user.displayName

    self.FacebookUser.text = name

    let storage = FIRStorage.storage()

    //refer your particular storage service
    let storageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com")
    let profilePicRef = storageRef.child(user.uid+"/profile_pic.jpg")

    profilePicRef.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) -> Void in
        if (error == nil){

            self.FacebookPic.image = UIImage(data: data!)

        }else{

            print("Error downloading image:" )


        }
    })

        if(self.FacebookPic.image == nil)
    {
        var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height": 300, "width": 300, "redirect": false], httpMethod: "GET")
        profilePic?.start(completionHandler: {(_ connection, result, error) -> Void in
            // Handle the result


            if error == nil {
                if let dictionary = result as? [String: Any],
                    let data = dictionary["data"] as? [String:Any],
                    let urlPic = data["url"] as? String{

                    if let imageData = NSData(contentsOf: NSURL(string: urlPic)!as URL){

                        let uploadTask = profilePicRef.put(imageData as Data, metadata: nil) {
                            metadata, error in

                            if (error == nil)
                            {
                                let downloadurl = metadata!.downloadURL
                            }
                            else
                            {
                                print("Error in downloading image")
                            }
                        }

                        self.FacebookPic.image = UIImage(data: imageData as Data)
                    }}}})}
}else{
}

//The END of the Facebook user and picture code

我能让它工作几天,但现在它不再工作了,我已经一行一行地看了一遍,我真的不明白为什么它不工作

您只需获得您的facebook个人资料图片。使用此url并将url放入您的UIImageview

let profilepicURl = "https://graph.facebook.com/\(user_id_fb)/picture?type=large" //user_id_fb like 1251246454544 your facebook ID
我使用了以下代码:

func pictureFromFirebase(loginMethod: Int)
{
    if loginMethod == 0 //FB
    {
        var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height":300, "width":300, "redirect":false], httpMethod: "GET")
        let profilePicRef = storageRef.child((user?.uid)!+"/profile_pic.jpg")
        profilePicRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                // Uh-oh, an error occurred!
                // but we don't need to do anything yet.  Try to download the profile pic
            }
            if (data != nil)
            {
                print("no need to download image from facebook")
                self.profileImage.image = UIImage (data: data!)
            }
            else
            {
                // THIS IS THE BLOCK THAT HAS BEEN MOVED
                // WHICH WILL NOW BE EXECUTED IN TWO CONDITIONS - 
                // 1. AN ERROR IN THE DOWNLOAD
                // 2. NO PROFILE PIC AVAILABLE
                print("downloading image from facebook")
                profilePic?.start(completionHandler: {(_ connection, _ result, _ error) -> Void in
                    if (error == nil)
                    {
                        if let dictionary = result as? [String:Any], let data = dictionary["data"] as? [String:Any],
                            let urlPic = data["url"] as? String {
                            if let imageData = NSData(contentsOf: NSURL(string: urlPic)! as URL)
                            {
                                let uploadTask = profilePicRef.put(imageData as Data, metadata: nil){
                                    metadata, error in
                                    if (error == nil)
                                    {
                                        let downloadUrl = metadata!.downloadURL
                                    }
                                    else
                                    {
                                        print("error in downloading image")
                                    }
                                }
                                self.profileImage.image = UIImage(data: imageData as Data)
                            }
                        }

                    }
                })
            }

        }
    }
}

从这篇文章中,它起作用了

请确保更新主线程上的图像。i、 e.执行此操作时:
self.FacebookPic.image=UIImage(数据:数据!)
@Scriptable我将如何执行此操作?