我的问题归结为:获取带有照片ID的照片网址。最终,我需要为我的iOS应用程序的用户标记的所有照片执行此操作。我可以使用代码获取照片的ID:
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"me", parameters:["fields": "photos"])
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil { }
else if let userData = result as? [String:AnyObject] {
let userPhotos = userData["photos"]
}
}
在返回的用户数据中,我可以访问每张照片的ID。现在,我定义了一个函数,该函数应该(理想情况下)返回给定ID的照片的URL。但是,我真的不知道如何正确调用Facebook Graph API。这就是我所拥有的(注意:硬编码的照片ID)。
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"/{photo-id}?id=xxxxxxxxxxxxxxxxx", parameters:["fields": "link"])
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil { }
else if let userData = result as? [String:AnyObject] {
let link = userData["link"]
print(link)
}
}
我非常有信心graphPath参数非常错误。我做了一些"智能"基于此的猜测:https://developers.facebook.com/docs/graph-api/reference/photo/
您可以给予我任何帮助,我将不胜感激。谢谢!
答案 0 :(得分:2)
好的,这是一个简单的答案,感谢上面的答案@ user2585945
导入UIKit
class FacePhotosViewController: UIViewController{
@IBOutlet weak var imgTest: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.currentAccessToken() != nil)
{
print("We are good!")
}
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"photos,picture"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print(result)
let resultdict = result.objectForKey("photos") as! NSDictionary
let data : NSArray = resultdict.objectForKey("data") as! NSArray
for i in 0..<data.count {
let valueDict : NSDictionary = data[i] as! NSDictionary
let id = valueDict.objectForKey("id") as! String
let created_time = valueDict.objectForKey("created_time") as! String
print("id: \(id) created_time: \(created_time) ")
}
}
})
let graphRequestPhoto : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "10156292340905363?type=large", parameters: ["fields":"picture, name, album, place"])
graphRequestPhoto.startWithCompletionHandler({ (connection, result, error) -> Void in
print(result)
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else if let userData = result as? [String:AnyObject] {
let url = userData["picture"] as? String
print("link: \(url)")
let imgURL: NSURL! = NSURL(string: url!)
//lets' download profile photo
let session = NSURLSession.sharedSession()//session
let request: NSURLRequest = NSURLRequest(URL: imgURL!)//request
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in //session calls the request
if let noerror = data {
dispatch_async(dispatch_get_main_queue()) {
let image = UIImage(data: noerror)
self.imgTest.image = image
}
}
else {
print("Error: \(error!.localizedDescription)", terminator: "")
}
}
dataTask.resume()
}
})
}
}
答案 1 :(得分:1)
let requestPhotos = FBSDKGraphRequest(graphPath:id_num, parameters:["fields":"picture, name, album, place"])
其中id_num
是Facebook照片的ID号。非常简单。