【问题标题】:TwitterKit for Swift not displaying ImagesTwitterKit for Swift 不显示图像
【发布时间】:2016-01-30 03:34:35
【问题描述】:

问题
将返回的 Twitter JSON 提要加载到 TwitterKit 表视图单元格时,不会加载个人资料图像和媒体图像。所有其他数据(用户名、转推状态、描述、链接)已加载并在 twitterTableView 中正确显示,但图像没有。

JSON 提要包含准确的图像 URL。

有没有人遇到过这种情况,或者有允许在视图控制器中嵌套 Twitter 表的解决方案?


版本信息
Xcode v7.0.1
代码库:Swift 2.0
应用兼容性 iOS 8.3+
TwitterCore v1.12.0
TwitterKit v1.12.0


情节提要中的 ViewController

  • 查看
    • 滚动视图
      • InfoView(用于其他信息)
      • SocialView(有关 Twitter 个人资料和 Twitter 源的信息)
        • 个人资料信息查看
        • twitterTable(未分配类,ViewController 作为数据源和委托)
          • twitterTableViewCell(分配的 TWTRTweetTableViewCell 类)


元数据工作流程

  1. 应用调用专有服务器 API
  2. 服务器:
    • 返回缓存的 Twitter JSON
    • 使用搜索词调用 Twitter API。缓存 Twitter JSON 响应。返回 Twitter JSON。
  3. App 解析 JSON,并利用 TWTRTweet.tweetsWithJSONArray 设置 var tweets
  4. tweets 集上,用 tweets 更新 twitterTable


ViewController 中的相关代码

import UIKit
import Alamofire
import SwiftyJSON
import RealmSwift
import TwitterKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, TWTRTweetViewDelegate {
    @IBOutlet weak var socialView: UIView!
    @IBOutlet weak var friendCollectionView: UICollectionView!
    @IBOutlet weak var twitterTableView: UITableView!

    @IBOutlet weak var friendColletionViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var twitterTableViewHeightConstraint: NSLayoutConstraint!


    var tweets: [TWTRTweet] = [] {
        didSet {
            twitterTableView.reloadData()
        }
    }
    var tweetHeights:[Int:CGFloat] = [:]

    var isLoadingTweets = false
    let tweetTableCellReuseIdentifier = "TweetCell"
    var prototypeCell: TWTRTweetTableViewCell?

    var isLoadingFriend = false
    var friends:[Friend] = [] {
        didSet {
            friendCollectionView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()        

        self.loadInfo()
        self.loadFriends()
        self.loadTweets()

        // Create a single prototype cell for height calculations.
        self.prototypeCell = TWTRTweetTableViewCell(style: .Default, reuseIdentifier: tweetTableCellReuseIdentifier)

        // Register the identifier for TWTRTweetTableViewCell.
        self.twitterTableView.registerClass(TWTRTweetTableViewCell.self, forCellReuseIdentifier: tweetTableCellReuseIdentifier)
    }

    func loadInfo(){
        print("loads basic info")
        // code removed to simplify post
    }

    func loadFriends(){
        print("loads friendsCollectionView inside Info")
        // code removed to simplify post
    }

    func loadTweets(){
        print("loadTweets")
        // Do not trigger another request if one is already in progress.
        if self.isLoadingTweets {
            return
        }
        self.isLoadingTweets = true

        Alamofire.request(APIService.Router.TwitterSearch(searchType: "hashtag", searchTerm: "something", userSocialProfileID: ""))
            .responseString { response in
                print("request: \(response.0!)")
                print("\n*******loadTweets********\n\n\(response.2.value!)\n\n**************\n")
            }
            .responseJSON { response in
                // print("\n*******loadTweets:JSON********\n\n\(response.2.value!)\n\n**************\n")

                if let json = response.2.value {
                    var jsonObj = JSON(json)

                    if let results = jsonObj["results"].dictionary{
                        if let jsonTweets = results["statuses"]!.arrayObject {
                            // print("The tweets are \n\(jsonTweets)")
                            self.tweets = TWTRTweet.tweetsWithJSONArray(jsonTweets) as! [TWTRTweet]
                        } else {
                            print("jsonTweets are not an array of Objects")
                        }
                    } else {
                        print("Results is not a dictionary.")
                    }

                    dispatch_async(dispatch_get_main_queue(),{
                        self.twitterTableViewHeightConstraint.constant = 0
                        for (_, tweetHeight) in self.tweetHeights {
                            self.twitterTableViewHeightConstraint.constant += tweetHeight
                        }
                        self.isLoadingTweets = false
                    })

                } else {
                    print("value was not json...or something is wrong with the code")
                }
        }

    }

    //MARK: UITABLEVIEW
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tweets.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let tweet = self.tweets[indexPath.row] as TWTRTweet!
        if tweets.count > indexPath.row {
            prototypeCell?.configureWithTweet(tweet)
        }

        let tweetHeight = TWTRTweetTableViewCell.heightForTweet(tweet, width: tableView.bounds.width)

        self.tweetHeights[indexPath.row] = tweetHeight
        return tweetHeight
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableCellReuseIdentifier, forIndexPath: indexPath) as! TWTRTweetTableViewCell

        // Assign the delegate to control events on Tweets.
        cell.tweetView.delegate = self

        cell.tweetView.showActionButtons = false
        cell.tweetView.linkTextColor = UIColor().cosMediumPurple()

        // Retrieve the Tweet model from loaded Tweets.
        let tweet = tweets[indexPath.row] as TWTRTweet!

        // Configure the cell with the Tweet.
        cell.configureWithTweet(tweet)

        // Return the Tweet cell.
        return cell
    }
}

【问题讨论】:

  • 感谢您提供出色的错误报告。

标签: ios swift twitter swift2


【解决方案1】:

此问题可能是由于 iOS 9 上不匹配的 SHA1 证书受到 Apple 新的 App Transport Security 的影响。您可以通过为您的 Info.plist 文件 (http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/) 提供 pbs.twimg.com 的 ATS 异常来解决此问题。

您可以通过使用带有--ats-diagnostics 选项的nscurl 命令加载图像来更准确地诊断问题:

nscurl --ats-diagnostics https://pbs.twimg.com/profile_images/2044921128/finals.png

由于只有部分 CDN 服务器拥有这些旧的 SHA1 证书,因此您不会总是看到问题。由于 Twitter Kit 正在为您处理图像缓存,因此图像加载失败的时间可能并不明显,因为它在过去某个时间点访问了正确配置的服务器。

以下是模拟器中出现问题的示例:

【讨论】:

    【解决方案2】:

    答案其实很烦人也很简单。 TWTRTweet 类不包含配置文件图像或附加媒体的变量。 Twitter 提供了方法:

    TWTRTweet.tweetsWithJSONArray()
    

    但该方法会立即丢弃您需要的图像的任何 URL。您需要构建一个自定义解析方法。我已经完成了一些 Twitter JSON 解析工作,您可以查看 here.

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 2019-09-17
      • 2018-06-05
      • 1970-01-01
      • 2014-11-29
      • 2019-09-23
      • 2018-09-04
      • 2022-07-05
      • 1970-01-01
      相关资源
      最近更新 更多