0

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

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

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

Twitter 在 iOS 上的 statuses/update_with_media 返回 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 "];

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

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

4

1 回答 1

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];
}
于 2013-03-25T13:33:08.953 回答