gpt4 book ai didi

ios - 在 Swift 2 中使用 Parse 获取 Twitter 个人资料图片

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:49 24 4
gpt4 key购买 nike

我正在尝试使用 swift 2 获取 Twitter 个人资料图片。

此链接描述了它是如何为 swift 完成的: Getting Twitter profile image with Parse in Swift

但问题是 NSURLConnection 现在已被弃用并被 NSURLSession 取代。 PFTwitterUtils.twitter()?.signRequest 需要一个 NSMutableURLRequest 但是从提到的替换同步请求的方法中我得到了一个 NSURLRequest。如何使代码工作?

我试图编写这段代码来让它工作,但被难住了:

     if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {

let screenName = PFTwitterUtils.twitter()?.screenName!


let requestString = NSURL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
let request = NSURLRequest(URL: requestString!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)


PFTwitterUtils.twitter()?.signRequest(request)

var response: NSURLResponse?
var error: NSError?



let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
print(data)
print(response)
print(error)
}).resume()



if error == nil {

let result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)


let names: String! = result?.objectForKey("name") as! String

let separatedNames: [String] = names.componentsSeparatedByString(" ")

//self.firstName = separatedNames.first!
//self.lastName = separatedNames.last!


let urlString = result?.objectForKey("profile_image_url_https") as! String

let hiResUrlString = urlString.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)


let twitterPhotoUrl = NSURL(string: hiResUrlString)
let imageData = NSData(contentsOfURL: twitterPhotoUrl!)
let twitterImage: UIImage! = UIImage(data:imageData!)
}

NSURLConnection throws after updating to Swift 2.0

最佳答案

根据您的代码,我能够使用以下方法获取要检索的 Twitter 图片:

if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {

let screenName = PFTwitterUtils.twitter()?.screenName!
let requestString = NSURL(string: "https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
let request = NSMutableURLRequest(URL: requestString!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
PFTwitterUtils.twitter()?.signRequest(request)
let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
print(data)
print(response)
print(error)

if error == nil {
var result: AnyObject?
do {
result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
} catch let error2 as NSError? {
print("error 2 \(error2)")
}

let names: String! = result?.objectForKey("name") as! String
let separatedNames: [String] = names.componentsSeparatedByString(" ")

//self.firstName = separatedNames.first!
//self.lastName = separatedNames.last!

let urlString = result?.objectForKey("profile_image_url_https") as! String
let hiResUrlString = urlString.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let twitterPhotoUrl = NSURL(string: hiResUrlString)
let imageData = NSData(contentsOfURL: twitterPhotoUrl!)
let twitterImage: UIImage! = UIImage(data:imageData!)
}
}).resume()
}

我将结果处理整合到数据任务中,并修复了 Swift 2 的 JSONObjectWithData:options: 调用。

关于ios - 在 Swift 2 中使用 Parse 获取 Twitter 个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326428/

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