在 Swift 5 中使用图 API 显示用户的 Facebook 图片
Showing a user's Facebook Picture using Graph API in Swift 5
我的应用程序可以成功检索用户的个人资料图片 URL 作为 link 并将其打印到控制台。但是,我将此 link 转换为在故事板文件的 UIImageView 中显示的图像。
代码如下:
import Foundation
import FacebookCore
import FacebookLogin
class HomeAfterLogInViewController: UIViewController
{
// Links to UIImageView on storyboard
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let connection = GraphRequestConnection()
connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in
if let dictData: [String : Any] = userresult as? [String : Any]
{
DispatchQueue.main.async
{
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
{
if let data : [String: Any] = pictureData["data"] as? [String: Any]
{
// prints out url of profile
print(data["url"] as! String)
}
}
}
}
})
connection.start()
}
}
我找到了几段代码,但 none 有效。
您可以使用这个名为 Haneke 的库,它非常适合缓存并添加了一些有用的方法,安装后您可以执行类似的操作
guard let url = URL(string: urlAsString) else { return }
yourImageView.hnk_setImageFromURL(url)
整个问题的答案是:
import Foundation
import FacebookCore
import FacebookLogin
import Haneke
class HomeAfterLogInViewController: UIViewController
{
// Links to UIImageView on storyboard
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let connection = GraphRequestConnection()
connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in
if let dictData: [String : Any] = userresult as? [String : Any]
{
DispatchQueue.main.async
{
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
{
if let data : [String: Any] = pictureData["data"] as? [String: Any]
{
// prints out url of profile
print(data["url"] as! String)
guard let url = URL(string: (data["url"] as! String)) else { return }
self.imageView.hnk_setImageFromURL(url)
}
}
}
}
})
connection.start()
}
}
我的应用程序可以成功检索用户的个人资料图片 URL 作为 link 并将其打印到控制台。但是,我将此 link 转换为在故事板文件的 UIImageView 中显示的图像。
代码如下:
import Foundation
import FacebookCore
import FacebookLogin
class HomeAfterLogInViewController: UIViewController
{
// Links to UIImageView on storyboard
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let connection = GraphRequestConnection()
connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in
if let dictData: [String : Any] = userresult as? [String : Any]
{
DispatchQueue.main.async
{
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
{
if let data : [String: Any] = pictureData["data"] as? [String: Any]
{
// prints out url of profile
print(data["url"] as! String)
}
}
}
}
})
connection.start()
}
}
我找到了几段代码,但 none 有效。
您可以使用这个名为 Haneke 的库,它非常适合缓存并添加了一些有用的方法,安装后您可以执行类似的操作
guard let url = URL(string: urlAsString) else { return }
yourImageView.hnk_setImageFromURL(url)
整个问题的答案是:
import Foundation
import FacebookCore
import FacebookLogin
import Haneke
class HomeAfterLogInViewController: UIViewController
{
// Links to UIImageView on storyboard
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let connection = GraphRequestConnection()
connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in
if let dictData: [String : Any] = userresult as? [String : Any]
{
DispatchQueue.main.async
{
if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
{
if let data : [String: Any] = pictureData["data"] as? [String: Any]
{
// prints out url of profile
print(data["url"] as! String)
guard let url = URL(string: (data["url"] as! String)) else { return }
self.imageView.hnk_setImageFromURL(url)
}
}
}
}
})
connection.start()
}
}