在iphone上将图片上传到twitter

时间:2013-03-25 12:21:42

标签: iphone twitter

如何使用此方法将图像上传到Twitter

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType

我从stackoverflow.com获得这个方法,链接在这里,

Twitter's statuses/update_with_media on iOS returns 500 error

在上面的链接中,有4位成员表示很棒,他们是Ahmed,olivarseF,Gypsa,另一位是Tk189。

这里的代码如下,

在这里输入代码

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{

        NSString *boundary = @"----------------------------991990ee82f7";

        NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
        if (!finalURL) 
        {
            return nil;
        }

        NSLog(@"-> Open Connection: %@", finalURL);


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

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

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


        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

        NSMutableData *body = [NSMutableData dataWithLength:0];
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media_data[]\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
        [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\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];

        [theRequest setHTTPBody:body];

        // Create a connection using this request, with the default timeout and caching policy, 
        // and appropriate Twitter request and response types for parsing and error reporting.
        MGTwitterHTTPURLConnection *connection;
        connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                                                                delegate:self 
                                                             requestType:requestType 
                                                            responseType:responseType];

        if (!connection) 
        {
            return nil;
        } 
        else 
        {
            [_connections setObject:connection forKey:[connection identifier]];
            //[connection release];
        }

        return [connection identifier];  
    }

在上面的代码中,它集成在SA_OAuthtwitterengine类和这一行中,

[body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];

给了我错误报告,所以我导入了NSData + Base64文件,错误很明显。

然后我尝试使用此行发送图像[_engine sendupdate:@“http://www.xxxx.com/ccc.jpg”];

这里我希望将图像作为网址发送,但在这里我收到此错误“错误域= HTTP代码= 401”操作无法完成。 (HTTP错误401。)“

为什么呢?如果你知道这个答案,请如何发送这个,特别是我要求你先生(ahmed,oliversf,gypsa和tk189)请解释你如何在saouthtwitter引擎课程中使用。

1 个答案:

答案 0 :(得分:0)

您使用的是特定框架吗? 如果您的应用程序支持应用程序支持> = iOS 5,您可以使用已集成在iOS SDK中的社交框架,如下所示:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    NSString *title = [self.elementInfo objectForKey:@"title"];
    [tweetSheet setInitialText:@"message you want to display"];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}