问题描述
我正在尝试使用带有自定义UI的Twitters新TwitterKit发布图像和推文。他们提供的唯一文档是如何使用他们的视图。
I am trying to post an image and tweet using Twitters new TwitterKit with a custom UI. The only documentation they provide is how to do it with their views.
所以我可以弄明白没有图像的方法
so I can figure out how to do it without an image
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links", nil];
NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];
[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){
}];
但是他们的URLRequestWithMethod方法不可变。我将如何添加图像。您以前使用SLRequest执行此操作
But their URLRequestWithMethod method isnt mutable. how would I add an image to it. You used to do it with the SLRequest with
[postRequest addMultipartData:UIImageJPEGRepresentation(image, 0.5) withName:@"media" type:@"image/jpeg" filename:@"image.png"];
推荐答案
我已经弄明白了。
首先,您需要将图片发布到Twitter。
First you need to post the image to twitter.
NSString *media = @"https://upload.twitter.com/1.1/media/upload.json";
NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
NSString *imageString = [corgiData base64EncodedStringWithOptions:0];
NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:media parameters:@{@"media":imageString} error:&requestError];
[[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {
}];
然后在响应对象中使用media_id_string并将其添加到我的问题中的代码参数。
then in the response object you use the media_id_string and add that to the parameter of the code in my question.
所以
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",mediaIDString, @"media_ids", nil];
NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];
[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];
}];
请注意来自第一个请求的响应的media_ids对象
note the media_ids object that is from the response of the first request
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",[responseDict objectForKey:@"media_id_string"], @"media_ids", nil];
所以你可以把它放在完成块内,它会发布图像和推文。
So you can just put that inside the completion block and it will post the image and tweet.
这篇关于使用twitterkit将图像发布到twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!