views:

543

answers:

3

I want to upload an image to my twitter profile using objective-c. I saw in the twitter API that I need to send a HTML post to http://twitter.com/account/update_profile_image.format and send the picture as a parameter. I am done with the authentication. I am stuck with the uploading. Maybe somebody can help me with sending the picture as a parameter?

A: 

If you’ve managed to get started, then this post on CocoaDev should help you set the uploading up. There’s a sample linked at the top too.

Ciarán Walsh
+1  A: 

You should be using NSURLRequests and NSURLConnection to perform the API requests. If this is true, all you need to do is create an NSMutableURLRequest, set it's URL to the Twitter image upload API URL, set the method to POST.

Then you'll need to create an NSData object to represent your image, which you can do using

NSData *myImageData = [[NSData alloc] initWithData:[myImage CGImage]];

I don't know what the parameter name is for Twitter's upload API, so for arguments sake, lets call it "image". The next thing you need to do is set the image data as the request's body for the "image" parameter, like this

NSString *bodyString = [NSString stringWithFormat:@"image=%@", [[[NSString alloc] initWithData:myImageData encoding:NSStringUTF8Encoding] autorelease]];
[myRequest setBody:bodyString];

Then you can just start your NSURLConnection with the request and it should upload.

Jasarien
A: 

I recommend using ASIHTTPRequest

What is ASIHTTPRequest?

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.

It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.

See this blog post for an example

epatel