2013-03-25 67 views
0

叽叽喳喳如何使用这种方法上传图片到iphone

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

我从stackoverflow.com这种方法是这样的链接在这里,

Twitter's statuses/update_with_media on iOS returns 500 error上传图片到Twitter。

在上面的链接,它是伟大的说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引擎中的使用方法一流

+0

@bhuXan你能不能知道上述问题的解决方案请帮助我这是非常紧急的。 – raman 2013-03-25 13:24:22

+0

任何人在这里请帮助我很紧急 – raman 2013-03-25 13:32:46

+0

拉曼:你检查了这个朋友,https://dev.twitter.com/discussions/15177 – BhushanVU 2013-03-25 13:33:43

回答

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]; 
} 
+0

我的应用程序支持从最低部署目标是3.0(ios,ipod)。所以我使用MGTwitterengine,SA_OAuthtwitterengine和OAuthconsumer类。我成功完成了发布到Twitter帐户的文本。但过去两个月,我试图在Twitter上发布图像。我没有明确的解决方案。 – raman 2013-03-25 13:38:35