how to share image to twitter from iphone application
<p>I'm using twitter+OAuth with MGTwitterengine in my application and successfully post a comment to twitter. But now my problem is share image to twitter using   </p>

<p><a href="https://upload.twitter.com/1/statuses/update_with_media.json" rel="nofollow">https://upload.twitter.com/1/statuses/update_with_media.json</a> . I searched many web sites and get source code by using this direct upload to twitter api     </p>

<p><a href="https://upload.twitter.com/1/statuses/update_with_media.json" rel="nofollow">https://upload.twitter.com/1/statuses/update_with_media.json</a> . here the code is below.</p>

<pre><code>-(void)UploadimageToTwitter
{

    //NSString *boundary = @"----------------------------991990ee82f7";
    // NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1.1/statuses/update_with_media.json"];

    NSString *accessTokenKey = kOAuthaccessTokenKey;

NSString *secretTokenKey = kOAuthsecretTokenKey;

    NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];

    /*if (!finalURL) 
     {
     return nil;
     }*/

    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];
    //Shearing picture on twitter with Oauth without any third party api.


    OAToken *token = [[OAToken alloc] initWithKey:accessTokenKey secret:secretTokenKey]; //Set user Oauth access token and secrate key

    //OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key

    OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL consumer:consumer token:token realm: nil signatureProvider:nil];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setTimeoutInterval:120];
    [theRequest setHTTPShouldHandleCookies:NO];

    // Set headers for client information, for tracking purposes at Twitter.
    [theRequest setValue:DEFAULT_CLIENT_NAME    forHTTPHeaderField:@"X-Twitter-Client"];
    [theRequest setValue:DEFAULT_CLIENT_VERSION forHTTPHeaderField:@"X-Twitter-Client-Version"];
    [theRequest setValue:DEFAULT_CLIENT_URL     forHTTPHeaderField:@"X-Twitter-Client-URL"];

    NSString *boundary = @"--0246824681357ACXZabcxyz";// example taken and implemented.
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

    // NSMutableData *body = [NSMutableData dataWithLength:0];
    NSMutableData *body=[NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    //status
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    //[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"%@",@"Latest Uploading"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];

    //[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    //media

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"media_data[]\"; filename=\"sunflower.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"index.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    UIImage *image1=[UIImage imageNamed:@"sunflower.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image1, 1.0); 

    // [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
    // [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];



    // [body appendData:[[NSString stringWithString:@"Honeymoon uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // --------------------------------------------------------------------------------
    // modificaiton from the base clase
    // our version "prepares" the oauth url request
    // --------------------------------------------------------------------------------
    [theRequest prepare];
    NSString *oAuthHeader = [theRequest valueForHTTPHeaderField:@"Authorization"];
    [theRequest setHTTPBody:body];

    NSHTTPURLResponse *response = nil;
    NSError *error = nil;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&amp;response error:&amp;error];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"oAuthHeader = %@ =",oAuthHeader);
    NSLog(@"ResponsString = \n%@",responseString);
}
</code></pre>

<p>Using this code i'm getting responsestring is below </p>

<pre><code>{"errors":[{"message":"Internal error","code":131}]}
</code></pre>

<p>It is a server error or my error. really do not know where i was wrong in the above code.</p>

<p>if anybody know answer please guide to me. your help saved my life cause i was searching the above task past three months using three kind of different api's like Twitpic, yfrog and now post/update_with_media. </p>
