开发者

Twitter profile image upload in objective-c

开发者 https://www.devze.com 2022-12-14 02:20 出处:网络
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 pictu

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 p开发者_开发知识库arameter?


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.


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.


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


Somthing like this

// See http://groups.google.com/group/twitter-development-talk/browse_thread/thread/df7102654c3077be/163abfbdcd24b8bf

NSString *postUrl = @"http://api.twitter.com/1/account/update_profile_image.json";
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL 
URLWithString:postUrl]];
[req addRequestHeader:@"Authorization" value:[oAuth oAuthHeaderForMethod:@"POST" 
andUrl:postUrl andParams:nil]];

[req setData:UIImageJPEGRepresentation(imageView.image, 0.8)
withFileName:@"myProfileImage.jpg"
andContentType:@"image/jpeg" forKey:@"image"];    

[req startSynchronous];
NSLog(@"Got HTTP status code from Twitter after posting profile image: %d", [req   
responseStatusCode]);
NSLog(@"Response string: %@", [req responseString]);
[req release];
0

精彩评论

暂无评论...
验证码 换一张
取 消