在iPhone中将图片上传到Twitter [英] upload image to twitter in iphone
问题描述
如何使用此方法将图像上传到Twitter
how to upload image to twitter by using this method
- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
我从stackoverflow.com获得此方法,链接在这里,
i get this method from stackoverflow.com that's the link is here,
Twitter在iOS上的状态/update_with_media返回500错误.
在上面的链接中,它是为说好的4个成员而工作的,它们分别是Ahmed,olivarseF,Gypsa,另一个是Tk189.
in the above link, it's work for great said 4 members, they are Ahmed, olivarseF, Gypsa and another one is 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类和此行中,
In the above code it was integrated in SA_OAuthtwitterengine class and this line,
[body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
为我提供了错误报告,因此我导入了NSData + Base64文件,错误已清除.
gave me the error report so i import NSData+Base64 file the error was clear.
然后我尝试使用此行发送图片[_engine sendupdate:@" http://www.xxxx.com/ccc.jpg ];
then i try to send image using this line [_engine sendupdate:@"http://www.xxxx.com/ccc.jpg"];
在这里我想将图像发送为url,但是在这里我收到此错误"Error Domain = HTTP Code = 401"该操作无法完成.(HTTP错误401.)"
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. (HTTP error 401.)"
为什么?如果您知道这个答案,请如何发送此邮件,尤其是我要求您(艾哈迈德,oliverrsf,gypsa和tk189)先生,请解释一下如何在saouthtwitter引擎类中使用.
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.
推荐答案
您是否正在使用特定的框架?如果您的应用程序支持应用程序支持> = iOS 5,则可以使用已集成在iOS SDK中的Social框架,如下所示:
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:
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];
}
这篇关于在iPhone中将图片上传到Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!