Facebook iOS SDK and swift: how get user's profile picture

前端 未结 10 1249
天涯浪人
天涯浪人 2021-01-31 03:51

i have integrated Facebook sdk in Xcode 6 (with swift). During the login i request the public_profile permission:

FBSession.openActiveSessionWithReadPermissions(         


        
相关标签:
10条回答
  • 2021-01-31 04:45

    For swift this is the simple way for get the url of the photo with and specific size:

        let params: [NSObject : AnyObject] = ["redirect": false, "height": 800, "width": 800, "type": "large"]
        let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture", parameters: params, HTTPMethod: "GET")
        pictureRequest.startWithCompletionHandler({
            (connection, result, error: NSError!) -> Void in
            if error == nil {
                print("\(result)")
    
    
               let dictionary = result as? NSDictionary
               let data = dictionary?.objectForKey("data")
               let urlPic = (data?.objectForKey("url"))! as! String
               print(urlPic)
    
    
    
            } else {
                print("\(error)")
            }
        })
    
    }
    
    0 讨论(0)
  • 2021-01-31 04:46

    @Brandon Gao solution gave me a 200X200 thumbnail... To get a bigger size I used FBSDKProfile to get a path with size, that is also more modular and not hard-coded (although I did have to type in the graph.facebook.com part...)

    let size = CGSize(width: 1080, height: 1080)
    let path = FBSDKProfile.currentProfile().imagePathForPictureMode(.Normal, size: size)
    let url = "https://graph.facebook.com/\(path)"
    Alamofire.request(.GET, url, parameters: nil, encoding: ParameterEncoding.URL).response { 
        (request, response, data, error) -> Void in
        if  let imageData = data as? NSData,
            let image = UIImage(data: imageData) {
                self.buttonImage.setImage(image, forState: .Normal)
        }
    }
    

    Somehow I didn't get a 1080X1080 image though, FB gave me a 1117X1117... :\

    0 讨论(0)
  • 2021-01-31 04:50

    if you want get bigger picture , just replace "type = large" to width=XX&height=XX

    but the biggest picture you can get is original picture

    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] 
                                  initWithGraphPath:@"me/picture?width=1080&height=1080&redirect=false" 
                                  parameters:nil 
                                  HTTPMethod:@"GET"]; 
    
    [request startWithCompletionHandler:^(
                                FBSDKGraphRequestConnection *connection,
                                id result, 
                                NSError *error) { 
    if (!error) 
    { 
       NSLog(@"result = %@",result);
       NSDictionary *dictionary = (NSDictionary *)result; 
       NSDictionary *data = [dictionary objectForKey:@"data"]; 
       NSString *photoUrl = (NSString *)[data objectForKey:@"url"]; 
    } 
    else 
    { 
       NSLog(@"result = %@",[error description]); } 
    }];
    
    0 讨论(0)
  • 2021-01-31 04:50

    Thank you @Lyndsey Scott. For Kingfisher, please add enable request http to .plist file.

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>http://graph.facebook.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    Then set your user's picture profile to ImageView.

    let facebookId = "xxxxxxx"
    let facebookProfile: String = "http://graph.facebook.com/\(facebookId)/picture?type=large"
    let url: URL = URL(string: facebookProfile)!
    myImageView.kf.setImage(with: url)
    
    0 讨论(0)
提交回复
热议问题