在 Swift 中使用 Parse 获取 Twitter 个人资料图片 [英] Getting Twitter profile image with Parse in Swift

查看:18
本文介绍了在 Swift 中使用 Parse 获取 Twitter 个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 中没有找到任何关于此的信息,所以我在问!如何使用 Parse 从 Twitter 获取登录用户的个人资料图片?

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

推荐答案

A这里是 Swift 版本.我不得不将用户名从 Parse 更改为 PFTwitter 的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆