mejim707
mejim707

Reputation: 441

Upload JPG image to Twitter

I'm trying to upload a jpg image to twitter. I can upload the @"icon.png" as many times as I want but can't seem to get a jpg to upload. I always get error 403 back from twitter. This is my code:

ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:0];


UIImage *image =[UIImage imageNamed:file]; //file is the name of a jpg image. If replaced with @"icon.png" the upload works


TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] 
                                             parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST];

[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"]; //I've tried this with UIImageJPEGRepresentation(image, 80) and multipart/jpg and jpeg to no avail


// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
 {
     NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
 }];

Please help!

If I use the below it works

UIImage *image =[UIImage imageNamed:@"background.png"]; //This file is copied to the device when the app is built.

But, if I use this is does not work, even though I have confirmed that this path and file name does intact pull up the image

UIImage *image =[UIImage imageNamed:[NSString stringWithFormat:@"%@/%@", dir, file]];

What am I doing wrong? Thanks

/// SOLUTION I may be an idiot, please don't crucify me, but here is what I needed to change the UIImage to

UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", dir, file]];

This allows both PNG and JPG to upload without issue. Stupid mistake but at least it's working now. Thanks for the replies!!

Upvotes: 0

Views: 1146

Answers (1)

user466679
user466679

Reputation: 11

The 403 error means the error comes from Twitter; you cannot share the same content twice from the same Twitter account. If you want to upload the same picture, then you must first remove it from the Twitter account, then try to upload it again. Otherwise, upload a different picture.

Upvotes: 1

Related Questions