我正在尝试使用新的api状态/ update_with_media在Twitter上分享图像。然后我搜索并从dev.twitter.com/discussion页面获得以下代码。这里的代码如下。
在这里输入代码
- (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中实现了这个代码,因为我使用oauth方法将文本或图像发布到twitter,因为我的应用程序部署目标是从3.0(ios,ipod)开始,所以我尝试使用这种方法。
现在我的问题是如何使用消息创建发送图像到twitter的代码。
我尝试使用以下类型将图像发送到Twitter
[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];
我收到连接错误,表示401错误。
请您使用上面的代码向我发送如何将图像发送到Twitter的信息?
答案 0 :(得分:0)
以下是使用新的twitter api update_with_media进行图片上传的解决方案。使用此api,您可以直接将图像上传到Twitter帐户。上面的_uploadimage代码应首先放在SA_OAuthtwitter引擎.m文件中,并在.h文件中进行decalred。
并设置一个小的正确位置,其中放置了filenamed = 1.jpg而不是1.png。
现在只需将以下代码放在MGTwitterengine.m类中。这里的代码在
之下 - (NSString *)_uploadImage:(UIImage *)image withStatus:(NSString *)status
{
return [self _uploadImage:image withStatus:status requestType:MGTwitterImageRequest responseType:MGTwitterStatuses];
}
然后我们使用此代码将图片上传到twitter。就是这样,
[_ engine _uploadImage:yourpicture withStatus:yourstatus];
希望它对你有用....