Search code examples
iosswiftparse-platformnsdata

Saving picture result from FBSDKGraphRequest as PFFile


I'm registering my users with Facebook SDK. I'm trying to save users profile picture using FBSDKGraphRequest. What I get is the following result:

{
    email = "adolfosrs@gmail.com";
    id = 1053423191343936;
    name = "Adolfo Schneider";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/hprofile-xpa1/v/t1.0-1/p50x50/12144940_1046320742054181_1730939492302855415_n.jpg?oh=bb3a3fa2fb174147ac0a0b41f6dd&oe=56C016A2";
        };
    };
}

So I need to save result["picture"] as a PFFile on Parse. But I just cant figure out how to do it. I need to somehow convert the result["picture"] to a NSData to initialize the PFFile and finally save it.

Any ideas?


Solution

  • Just realized. We must use NSURL and in addition NSData:contentsOfUrl.

    let pictureData = result["picture"]!!["data"]
    let pictureUrl = pictureData!!["url"] as! NSString
    let imgURL = NSURL(string: pictureUrl as String)
    let imgData = NSData(contentsOfURL: imgURL!)
    let imageFile = PFFile(data: imgData!)