使用 URL 显示 Facebook 用户图片 (Swift 5 | Xcode 10)

Showing a Facebook User Picture using the URL (Swift 5 | Xcode 10)

我是 Swift 5 的新手,目前正在开发一个需要使用 Facebook 登录 SDK 的应用程序。用户可以成功登录,代码 returns 电子邮件和 URL 到用户的个人资料图片。但是,我希望个人资料图片显示在故事板上。

我的故事板名为:"HomeAfterLogInViewController.swift" 看起来像这样:

此文件中的代码是:

import Foundation
import FacebookCore
import FacebookLogin

class HomeAfterLogInViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        getFacebookProfileInfo()
    }
}

func getFacebookProfileInfo()
{
    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]
                        {
                            print(data)
                            print(dictData["email"]!)     
                      }
                  }
              }
          }
      })
      connection.start()
  }

我在网上找到这段代码:

leturl = NSURL(data)  // holds url from facebook
let data = NSData(contentsOfURL: url)

imageURL.image = UIImage(data: data)

但这给了我这个错误:

如何编写所需的代码?

 if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
 {
     if let data : [String: Any] = pictureData["data"] as? [String: Any]
      {
           //Picture Url 
           print(data["url"] as! String)
       }
 }

试试下面的代码。

func getFacebookProfileInfo() {
    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]
                        {
                            print(data)
                            print(dictData["email"]!)     

                            if let pictureURL = pictureData["url"] as? String { //image url of your image

                                   if let url = URL(string: pictureURL) {

                                       if let data = try? Data(contentsOf: url) { //here you get image data from url

                                          //generate image from data and assign it to your profile imageview
                                          imageView.image = UIImage(data: data) 
                                       }
                                   }
                            }
                      }
                  }
              }
          }
      })
      connection.start()
  }

我希望它能帮助您从 URL 获取图像。