iOS 5使用Twitter API上传照片到Twitter

7
有没有办法使用TWRequest或类似的方法向Twitter时间线添加照片?在这种情况下,我非常迷失。
我可以使用iOS5的TWRequest和先前iOS版本的MGTwitterEngine发布状态更新,但我无法将UIImage附加到更新中。
非常感谢您提供的任何帮助。
3个回答

15

我在这个网站上找到并使用了很多好的答案,感谢大家,想着是时候回馈一下了 :) 在Noah的回答基础上,这是最终对我有用的代码,可以使用TWRequest在推文中获取图像和文本...

            TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];

            UIImage * image = [UIImage imageNamed:@"myImage.png"];

            //add text
            [postRequest addMultiPartData:[@"I just found the secret level!" dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data"];
            //add image
            [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/form-data"];

            // Set the account used to post the tweet.
            [postRequest setAccount:twitterAccount];

            // Perform the request created above and create a handler block to handle the response.
            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
            }];

我发现在我的情境下使用 TWRequest 是最好的解决方案,因为我不想让用户在推特之前编辑帖子(这是使用 TWTweetComposeViewController 的问题)。


这段代码只适用于iOS5吗?还是我可以在更低的版本中使用它? - iPhone
仅适用于iOS5,如果您需要一个< iOS5的解决方案,那里有一些开源的Twitter库可能会对您有所帮助,例如MGTwitterEngine... https://github.com/mattgemmell/MGTwitterEngine/ ...虽然我自己没有尝试过。 - staticfiction
你可以使用DETweetComposeViewController在iOS 4中完成同样的事情。 - Dave Batton
我之前使用同样的代码在Twitter上发送消息,并且在我之前的应用程序中也正常工作,但是现在我想使用这段代码发送消息,但是它显示错误消息“HTTP响应状态:403”,那你能告诉我问题出在哪里吗? - iPhone
1
@Neel,Twitter账户的代码与此示例中的代码相同... https://dev59.com/fGsz5IYBdhLWcg3wJUjJ - staticfiction

4
你可以使用Twitter的TWTweetComposeViewController视图控制器,无需处理Twitter OAuth和帐户即可发布照片到Twitter。有关更多详细信息,请参见http://developer.apple.com/library/ios/#documentation/Twitter/Reference/TWTweetSheetViewControllerClassRef/Reference/Reference.html
主要问题是它是iOS 5的新功能,因此未升级的用户将无法使用它。
TWTweetComposeViewController* tweetView = [[TWTweetComposeViewController alloc] init];
[tweetView addImage:yourImage];

TWTweetComposeViewControllerCompletionHandler 
       completionHandler =
    ^(TWTweetComposeViewControllerResult result) {
        switch (result)
        {
            case TWTweetComposeViewControllerResultCancelled:
                NSLog(@"Twitter Result: canceled");
                break;
            case TWTweetComposeViewControllerResultDone:
                NSLog(@"Twitter Result: sent");
                break;
            default:
                NSLog(@"Twitter Result: default");
                break;
        }
        [self dismissModalViewControllerAnimated:YES];
    };
[tweetView setCompletionHandler:completionHandler];

1

使用TWRequest,您可以使用-addMultipartData:withName:type:方法,并使用来自例如UIImageJPEGRepresentation()方法的数据,通过Twitter API的statuses/update_with_media方法发布推文。

如果您正在使用TWTweetComposeViewController,则更简单:您可以使用-addImage:方法将一个或多个UIImage图像附加到其中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接