在Swift中使用Parse获取Twitter个人资料图像 [英] Getting Twitter profile image with Parse in Swift
问题描述
在Swift中没有找到任何关于此的内容,所以我要问! 如何从Twitter获取使用Parse登录的用户的个人资料图片?
Haven't found anything about this in Swift, so i'm asking! How can I get the profile Image of a logged in User with Parse from Twitter?
用户已经通过解析内部login-/signupController登录
The User is already logged in via the parse internal login-/signupController
var username = PFUser.currentUser()?.username
并且图像应存储在UIImage变量中
And the image should be stored in an UIImage variable
推荐答案
AHere是Swift版本.我必须将用户名从PFTwitter的Parse更改为screenName,并添加一行以获取图片的最高分辨率版本.请注意,由于我在登录后使用它来填充注册屏幕,因此将测试连接到Twitter帐户的应用程序帐户.即使在创建帐户后也添加了链接,这也很有用.
AHere is the Swift version. I had to change the username from Parse to screenName from PFTwitter, and added a line to get the highest resolution version of the picture. Note that as I'm using this to populate a sign up screen after logging in, this will test for the app account being connected to a Twitter account. Most probably this would work even if the link has been added after account creation, too.
if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {
let screenName = PFTwitterUtils.twitter()?.screenName!
let requestString = ("https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
let verify: NSURL = NSURL(string: requestString)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: verify)
PFTwitterUtils.twitter()?.signRequest(request)
var response: NSURLResponse?
var error: NSError?
let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!
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!)
}
您可能还想添加这样的东西来准备PFFile来保存到您的新用户中!
You probably want to add something like this to prepare a PFFile to save to your new user, too!
let cgImage = twitterImage.CGImage
let bitsPerComponent = CGImageGetBitsPerComponent(cgImage)
let bytesPerRow = CGImageGetBytesPerRow(cgImage)
let colorSpace = CGImageGetColorSpace(cgImage)
let bitmapInfo = CGImageGetBitmapInfo(cgImage)
let context = CGBitmapContextCreate(nil, 300, 300, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(300), height: CGFloat(300))), cgImage)
let scaledImage = UIImage(CGImage: CGBitmapContextCreateImage(context))
let imageUIImage = UIImageJPEGRepresentation(scaledImage, 0.6)
let imageFile: PFFile = PFFile(name: (PFUser.currentUser().objectId! + "profileImage.jpg"), data:imageUIImage)
这篇关于在Swift中使用Parse获取Twitter个人资料图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!