2015-01-25 20 views
17

tôi đã tích hợp sẵn SDK Facebook trong Xcode 6 (với tính năng nhanh). Trong login tôi yêu cầu sự cho phép public_profile:Facebook iOS SDK và nhanh chóng: cách nhận hình ảnh hồ sơ của người dùng

FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true, completionHandler: { 
... 
... 

Vì vậy, tôi yêu cầu thông tin của người dùng:

FBRequestConnection.startForMeWithCompletionHandler { (connection, user, error) -> Void in 
... 
... 

Tại sao đối tượng người dùng không chứa các hình ảnh cá nhân? Làm thế nào tôi có thể có được hình ảnh hồ sơ người dùng? Nó không phải là một phần của public_profile?

tôi nhận được các thông tin sau:

2015-01-25 01:25:18.858 Test[767:23804] { 
"first_name" = xxx; 
gender = xxx; 
id = xxxxxxxxx; 
"last_name" = xxxxxx; 
link = "https://www.facebook.com/app_scoped_user_id/xxxxxxxxx/"; 
locale = "xxxxx"; 
name = "xxxxxxx xxxxxxx"; 
timezone = 1; 
"updated_time" = "2013-12-21T18:45:29+0000"; 
verified = 1; 
} 

PS: xxx cho sự riêng tư

+0

Đối với xcode 8.3.3 và nhanh chóng-4 (beta) sử dụng: https://stackoverflow.com/a/45462114/1168602 –

Trả lời

42

Các hình ảnh cá nhân là trong thực tế công cộng và bạn có thể chỉ đơn giản bằng cách thêm id dùng tới địa chỉ hồ sơ hình ảnh url định của Facebook, ví dụ:

var userID = user["id"] as NSString  
var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large" 

Địa chỉ url cụ thể này sẽ trả về phiên bản "lớn" của ảnh tiểu sử của người dùng, nhưng có nhiều tùy chọn ảnh khác in the docs.

+0

Hi. Một vài tháng trễ nhưng, tôi có thể hỏi ... ở đâu trong tài liệu là những tùy chọn ảnh có sẵn? Tôi dường như không thể tìm thấy chúng trên Graph API 2.4: https://developers.facebook.com/docs/graph-api/reference/v2.4/user/picture –

+0

@ AlejandroIván Trang tài liệu tôi liên kết đến bây giờ nói " Tài liệu này đề cập đến phiên bản API đồ thị lỗi thời. Vui lòng sử dụng phiên bản mới nhất. API phiên bản phiên bản v2.2 ", vì vậy có lẽ các tùy chọn ảnh đó không còn khả dụng trong phiên bản 2.4. –

+2

Đó là những gì tôi nghĩ. Đáng ngạc nhiên, ngay cả khi không được ghi lại "chính thức", sử dụng 'type = large' hoặc một cái khác (tôi đang sử dụng' width = 150 & height = 150') dường như chỉ hoạt động tốt. Cảm ơn bạn! –

27

Với Facebook SDK 4.0, bạn có thể sử dụng:

Swift:

let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: nil) 
    pictureRequest.startWithCompletionHandler({ 
     (connection, result, error: NSError!) -> Void in 
     if error == nil { 
      println("\(result)") 
     } else { 
      println("\(error)") 
     } 
    }) 

Objective-C:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] 
            initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"] 
            parameters:nil 
            HTTPMethod:@"GET"]; 
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, 
              id result, 
              NSError *error) { 
    if (!error){ 
     NSLog(@"result: %@",result);} 
    else { 
     NSLog(@"result: %@",[error description]); 
    }}]; 
+0

Bạn đã lưu lại ngày của mình ... – csotiriou

+0

điều này đã chỉ cho tôi đúng hướng để nhận thông tin người dùng khác, xem: http://stackoverflow.com/questions/31314124/get-email-and-name-facebook-sdk-v4-4-0-swift – CularBytes

+0

id/hình ảnh? type = normal & redirect = false làm việc tuyệt vời cho tôi, cảm ơn bạn. redirect = false đã tự hỏi –

3

@Brandon Gao giải pháp đã cho tôi một hình ảnh thu nhỏ 200X200 ... Để có kích thước lớn hơn, tôi đã sử dụng FBSDKProfile để nhận đường dẫn kích thước, đó cũng là mô-đun hơn và không mã hóa cứng (mặc dù tôi đã phải gõ vào graph.facebook.com phần ...)

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

Bằng cách nào đó tôi đã không nhận được một hình ảnh 1080X1080 dù , FB đã cho tôi một 1117X1117 ...: \

6

nếu bạn muốn có được bức tranh lớn hơn, chỉ cần thay thế "type = lớn" với chiều rộng = XX & height = XX

nhưng bức tranh lớn nhất mà bạn có thể nhận được là bản gốc ảnh

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]); } 
}]; 
36

Nếu bạn muốn nhận được hình ảnh trong cùng một yêu cầu như phần còn lại của thông tin người dùng, bạn có thể làm tất cả trong một yêu cầu biểu đồ. Đó là một chút lộn xộn nhưng nó đập một yêu cầu khác.

Một hơn Swift 3 cách tiếp cận

let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"]) 
let _ = request?.start(completionHandler: { (connection, result, error) in 
    guard let userInfo = result as? [String: Any] else { return } //handle the error 

    //The url is nested 3 layers deep into the result so it's pretty messy 
    if let imageURL = ((userInfo["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String { 
     //Download image from imageURL 
    } 
}) 

Swift 2

let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"]) 
request.startWithCompletionHandler({ (connection, result, error) in 
    let info = result as! NSDictionary 
    if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String { 
     //Download image from imageURL 
    } 
}) 
+0

Cảm ơn bạn đây là một bài viết –

4

Đối nhanh chóng đây là cách đơn giản để lấy url của bức ảnh với và kích thước cụ thể:

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

bạn ca n sử dụng mã này Đối với Swift 3.0 để có được thông tin người dùng

func getFbId(){ 
if(FBSDKAccessToken.current() != nil){ 
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email,picture.type(large)"]).start(completionHandler: { (connection, result, error) in 
    guard let Info = result as? [String: Any] else { return } 


    if let imageURL = ((Info["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String { 
     //Download image from imageURL 
    } 
if(error == nil){ 
print("result") 
} 
}) 
} 
} 
Các vấn đề liên quan