Daniel Morrison
Daniel Morrison

Reputation: 19

How do you get an image from a "t.co" URL (Twitter specific images) for an iOS app?

normally if you want an image from a URL there are plenty of ways to do it from a standard URL like this one:

a) https://pbs.twimg.com/media/CFDqQYMWYAAB_R8.png:large

However, if you extract a Twitter feed into an iOS app, you get the text and "t.co" URL from the string, which looks like this:

b) http://t.co/aQ7Q9eavlO

Now Xcode can't grab an image from that URL, but it stands to reason that there should be a way to go deeper, and get the image in link "b" so that it opens it to the format of link "a" so that I can show the image referenced in the tweet in my app, and not an unusable URL that returns a blank screen instead of the image in my imageView

As per request, I have updated the question to include the process I am using to get the images from the URL links:

// I won't add the whole Twitter/Social application, just the highlights

dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData];

NSDictionary *tweet = _array[indexPath.row];

tweetLabel.text = tweet[@"text"];

// I use "NSRegularExpression" To extract JUST the url from the tweet

// t.co address you get from the twitter feed

NSURL *url1 = [NSURL URLWithString:@"http://t.co/aQ7Q9eavlO"];

// manually going to the page and getting the image URL

NSURL *url2 = [NSURL URLWithString:@"https://pbs.twimg.com/media/CFDqQYMWYAAB_R8.png:large"];

NSData *data = [NSData dataWithContentsOfURL : url]; // url1 gives nothing/ url2 gives the actual image

UIImage *image = [UIImage imageWithData: data];

tempView.ContentView.image = image;

Both links come from the same place, but one uses the Twitter API to get the URL Twitter wants to share, the other gets the actual file URL. I can't find anyway to programmatically do the same thing, with usual XCode methods or using the Twitter/social frameworks

Upvotes: 0

Views: 2177

Answers (1)

chedabob
chedabob

Reputation: 5881

The URL you're trying to download is not the image, it's just a link to a page for the tweet containing it.

Using this command:

curl -L -v http://t.co/aQ7Q9eavlO

You can see it's just plain HTML.

Upvotes: 0

Related Questions