【问题标题】:Showing a Facebook User Picture using the URL (Swift 5 | Xcode 10)使用 URL 显示 Facebook 用户图片 (Swift 5 | Xcode 10)
【发布时间】:2020-01-23 13:37:59
【问题描述】:

我是 Swift 5 的新手,目前正在开发一个需要使用 Facebook 登录 SDK 的应用程序。用户可以成功登录,代码将电子邮件和 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)

但这给了我这个错误:

如何编写所需的代码?

【问题讨论】:

  • 首先,您需要通过@IBOutlet 将您的profileImageView 连接到HomeAfterLogInViewController,然后将UIImage(data: data) 分配给该profileImageView。检查here 如何将 UI 元素连接到您的 viewController。
  • 我是在 'func getFacebookProfileInfo()' 或 'class HomeAfterLogInViewController: UIViewController' 中还是在两者之外连接它?
  • 您在class 中连接它们,但这已经是另一个问题了。您必须浏览 stackOverflow 并搜索如何在 XCode 中连接 IBOutlets。

标签: ios swift facebook-graph-api facebook-sdk-4.0


【解决方案1】:

试试下面的代码。

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

希望对您从网址获取图片有所帮助。

【讨论】:

    【解决方案2】:
     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)
           }
     }
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 1970-01-01
      • 2019-07-21
      • 2013-11-09
      • 2019-12-19
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多