简体   繁体   English

在iPhone中将图片上传到Twitter

[英]upload image to twitter in iphone

how to upload image to twitter by using this method 如何使用此方法将图像上传到Twitter

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

i get this method from stackoverflow.com that's the link is here, 我从stackoverflow.com获得此方法,链接在这里,

Twitter's statuses/update_with_media on iOS returns 500 error . Twitter在iOS上的状态/ update_with_media返回500错误

in the above link, it's work for great said 4 members, they are Ahmed, olivarseF, Gypsa and another one is Tk189. 在上面的链接中,该工作适用于说4位成员,他们分别是Ahmed,olivarseF,Gypsa,另一个是Tk189。

here the code is below, 这是下面的代码,

enter code here 在这里输入代码

- (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];  
    }

In the above code it was integrated in SA_OAuthtwitterengine class and this line, 在上面的代码中,它已集成到SA_OAuthtwitterengine类中,并且此行中,

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

gave me the error report so i import NSData+Base64 file the error was clear. 给了我错误报告,所以我导入了NSData + Base64文件,错误很明显。

then i try to send image using this line [_engine sendupdate:@" http://www.xxxx.com/ccc.jpg "]; 然后我尝试使用此行发送图像[_engine sendupdate:@“ http://www.xxxx.com/ccc.jpg ”];

here i want sent image as a url, but here i got this error " Error Domain=HTTP Code=401 "The operation couldn't be completed. 在这里,我想将图像作为url发送,但是在这里,我收到此错误“ Error Domain = HTTP Code = 401”,操作无法完成。 (HTTP error 401.)" (HTTP错误401。)”

why? 为什么? how to send this one please if u know this answer and especially i request you to mr.(ahmed, oliversf, gypsa and tk189) please explain how you use in saouthtwitter engine class. 如果您知道这个答案,请如何发送此邮件,尤其是我要求您向先生(艾哈迈德,oliverrsf,gypsa和tk189)致谢,请解释一下如何在saouthtwitter引擎课程中使用。

Are you using a particular framework? 您使用的是特定框架吗? If your application support app support >= iOS 5 you can use Social framework already integrated in the iOS SDK with something like this: 如果您的应用程序支持应用程序支持> = iOS 5,则可以使用已经集成在iOS SDK中的Social框架,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM