gpt4 book ai didi

ios - Swift 的 TwitterKit 不显示图像

转载 作者:行者123 更新时间:2023-11-28 06:56:44 26 4
gpt4 key购买 nike

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

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

有没有人遇到过这种情况,或者有允许在 View Controller 中嵌套 Twitter 表的解决方案?


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


Storyboard中的 ViewController

  • 查看
    • ScrollView
      • InfoView(其他信息)
      • SocialView(有关 Twitter 个人资料和 Twitter Feed 的信息)
        • 个人资料 View
        • twitterTable(没有指定的类,以 ViewController 作为数据源和委托(delegate))
          • twitterTableViewCell(分配了 TWTRTweetTableViewCell 类)


元数据工作流程

  1. 应用程序调用专有服务器 API
  2. 服务器:
    • 返回缓存的 Twitter JSON
    • 使用搜索词调用 Twitter API。缓存 Twitter JSON 响应。返回 Twitter JSON。
  3. 应用解析 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
}
}

最佳答案

答案其实很烦人也很简单。 TWTRTweet 类不包含个人资料图像或附加媒体的变量。推特提供方法:

TWTRTweet.tweetsWithJSONArray()

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

关于ios - Swift 的 TwitterKit 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com