Search code examples
swifttwitterparse-platform

Getting Twitter profile image with Parse in Swift


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?

The User is already logged in via the parse internal login-/signupController

var username = PFUser.currentUser()?.username

And the image should be stored in an UIImage variable


Solution

  • 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!)
        }
    

    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)