在 Twitter 帖子中添加图片

4

如何像iPhone内置的照片应用程序那样,在Twitter帖子中附加一张图片? 如果有人有一些示例代码,那将是大大的帮助。 谢谢。


[tweetViewController 添加图片:img]; - Manu
4个回答

5
其他答案建议使用TWTweetComposeViewController,但是如果可以避免使用这个类,你应该这样做,因为它在iOS 6中已被弃用。
请参见这里:TWTweetComposeViewController在IOS6中已被弃用 并且来自苹果公司的消息,WWDC 2012, session 306 presentation PDF
Twitter Framework
• Twitter框架已被弃用 • 不要使用TWTweetComposeViewController 现在,要使用Twitter,您应该使用Social框架的SLComposeViewController类,其使用方式与TWTweetComposeViewController几乎相同。
您可能需要支持iOS 5,如果是这样,那么您唯一的选择就是使用TWTweetComposeViewController类,但您应该尽力检查SLComposeViewController是否可用,并在可用时使用它,因为这将为您节省时间和精力,当支持iOS 5被放弃时,TWTweetComposeViewController类真的可能会消失。如果您现在依赖Twitter框架来简化操作,因为它可以在iOS 5和6上运行,那么您的视野短浅,您以后肯定会遇到问题,只需再添加几行代码即可解决,并且这意味着您将不必担心未来的iOS SDK版本。
您应该导入Twitter.frameworkSocial.framework,并将它们都标记为可选导入(不是必需的)。
示例代码:
UIImage *myImage = [...]; // an image

if( NSClassFromString(@"SLComposeViewController") ){
    // We have the Social framework in our iOS system
    // iOS 6 and later will use this

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
        SLComposeViewController *twitterCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [twitterCompose addImage:myImage]; // Adding your UIImage

        twitterCompose.completionHandler = ^(SLComposeViewControllerResult result){
            // Handle result, dismiss view controller
            [self dismissViewControllerAnimated:YES 
                                     completion:nil];
        };

        [self presentViewController:twitterCompose
                           animated:YES
                         completion:nil];
    }else{
        // the user does not have Twitter set up
    }
}else if( NSClassFromString(@"TWTweetComposeViewController") ){
    // We don't have the Social framework, work with the Twitter framework
    // iOS 5 only will use this

    if( [TWTweetComposeViewController canSendTweet] ){
        TWTweetComposeViewController *twitterCompose = [[TWTweetComposeViewController alloc] init];

        [twitterCompose addImage:myImage];

        twitterCompose.completionHandler = ^(TWTweetComposeViewControllerResult result){
            // Handle result, dismiss view controller
            [self dismissViewControllerAnimated:YES 
                                     completion:nil];
        };
        [self presentViewController:twitterCompose
                           animated:YES
                         completion:nil];
    }else{
        // the user hasn't go Twitter set up on their device.
    }
}else{
    // Wow you're going retro with this app, 
    // you must be on iOS 4 if you ever get here...
}

你好,丹尼尔,如果您不介意的话,我需要更多帮助。现在我需要在Facebook上分享照片。我在网上搜索并找到了您上传视频到Facebook的示例代码FB,但我想知道如何将其用于分享照片以及如何在一个按钮中实现获取和上传方法,而不是两个按钮。我的意思是当用户点击分享按钮时,将被引导到权限页面,在授予我的应用程序权限后,照片将自动发布在他的墙上。我不确定是否可以完成这项任务。提前致谢。 - 4slices
@ChuckKelly,我不确定你为什么这样认为。我已经提供了自己的示例代码并引用了苹果和另一个SO问题。你觉得这个答案有什么问题,让你觉得它很糟糕? - Daniel
它依赖于一个库,我认为大多数查找此内容的人都希望获得允许他们在没有包含对话框的情况下发布推文的代码。 - ChuckKelly
也许这就是他们的建议,但是SLComposeViewController对于任何具有任何诚信的社交网络来说都是一个可怕的华而不实的解决方案。 - ChuckKelly
社交网络没有诚信的华而不实的解决方案...好吧,这有什么是客观的呢...我实际上认为大多数iOS用户都希望获得他们熟悉的iOS体验。所以如果你不同意这一点,那么祝你和你的应用好运。 - Daniel
显示剩余15条评论

0

这是我如何使用它的方法:

        NSLog(@"Ready to Tweet."); 
        TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
        [tweetComposer setInitialText:[NSString stringWithFormat:@"My message"]];
        [tweetComposer addImage:[UIImage imageNamed:@"114x114"]];
        [tweetComposer addURL:[NSURL URLWithString:@"http://myPage"]];
        tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
        if(result == TWTweetComposeViewControllerResultDone){
            NSLog(@"Tweeted.");

        } else if(result == TWTweetComposeViewControllerResultCancelled) {
            NSLog(@"Cancelled.");
        }

        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        };
        [self presentModalViewController:tweetComposer animated:YES];

你知道我需要在网站上上传图片并只发布其链接吗?如果您的代码可以实现这一点,那么请告诉我如何将我的应用程序中存在的图像附加到推特帖子中?提前致谢。 - 4slices
你不应再使用TWTweetComposeViewController,因为它已经被弃用。你应该使用SLComposeViewController类,它的使用方式几乎相同。 - Daniel
你的应用程序中有一个 UIImageSLComposeViewControllerTWTweetComposeViewController 处理上传到 Twitter。但是请查看我的答案,因为你应该避免使用 TWTweetComposeViewController:https://dev59.com/umnWa4cB1Zd3GeqP2qRc#12907850 - Daniel
我不同意...问题更加复杂。如果你支持iOS 4.3及以上版本(就像我一样),最好使用TWTweetComposeViewController(请注意,推文仅适用于iOS5及以上版本)。如果你只计划使用iOS 6,则SLComposeViewController更好。 - Frank

0
如果您正在使用iOS 5.0,则可以直接发布图片,如下所示:

添加Twitter.framework框架

导入Twitter/TWTweetComposeViewController.h

-(void)postToTwittert
{
    Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

    if (TWTweetComposeViewControllerClass != nil) {
        if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
            TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

            [twitter setInitialText:@"text"];
            [twitter addImage:[UIImage imageNamed:@"imagename"]];
            [twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];

            [self presentViewController:twitter animated:YES completion:nil];

            twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

                if(res == TWTweetComposeViewControllerResultDone)
                {
                    NSLog(@"success for twitter post");

                }
                else if(res == TWTweetComposeViewControllerResultCancelled)
                {

                    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Canceled" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

                    [alertView show];

                }

                [self dismissModalViewControllerAnimated:YES];

            };
        }
    }
}

在你想要发布推特的地方调用这个函数

并进行你想要的适当更改..

祝你好运..


0

我现在只是使用UIActivityViewController来发布推特。

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Default Text", [UIImage imageNamed:@"imageName"]] applicationActivities:nil];

[self presentViewController:controller animated:YES completion:nil];

这将展示一个控制器,用户可以决定要做什么(发布到 Twitter、发布到 Facebook 等等)

然后它使用系统的 tweetaheet 等来完成。

您无需提供默认文本。这可以被覆盖。

哦,还有,这不需要任何框架。


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