2013-03-25 65 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