将NSData附件添加到Twitter作为图像发布

时间:2014-03-21 01:46:57

标签: objective-c image twitter

目前我的应用程序提供了保存到设备或电子邮件的选项,后者将图像自动附加到邮件,我希望添加一个帖子到Twitter选项,只需附加图像并发布到Twitter,我已经完成这几次与其他应用程序,但似乎无法使这一个工作。

以下是电子邮件的处理过程;

-(void)displayComposerSheet 
{
     MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
     mail.mailComposeDelegate = self;

     [mail setSubject:@""];

     NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/email.png"];
     [mSplashView SaveResultImage:path];

     [mail addAttachmentData:[NSData dataWithContentsOfFile:path]  mimeType:@"image/png" fileName:@"Attached image"];

     NSString *msg = [NSString stringWithFormat:@"I made this image!", [UIDevice currentDevice].model];

     NSString* mailcontent =  [NSString stringWithFormat:@"<br> %@ <br>", msg];
     [mail setMessageBody:mailcontent isHTML:YES];

     [self presentModalViewController:mail animated:YES];

     [mail release];
}

我很难看到如何使用类似的方法将图片附加到Twitter,我目前使用此代码,但在尝试发布时崩溃;

    TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
    [self presentViewController:twitter animated:YES completion:nil];

   NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/email.png"];
    [mSplashView SaveResultImage:path];


    [twitter setInitialText:@"I made this image!"];
    [twitter addURL:[NSData dataWithContentsOfFile:path]];

    twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

        if(res == TWTweetComposeViewControllerResultDone) {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Thank you" message:@"Posted successfully to Twitter." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

            [alert show];

        }
        if(res == TWTweetComposeViewControllerResultCancelled) {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Cancelled" message:@"You Cancelled posting the Tweet." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

            [alert show];

        }

        [self dismissViewControllerAnimated:YES completion:nil];

    };


}

通常情况下,我只需拨打[twitter addImage:];,但不幸的是,如果不通过邮件表中的上述流程,图片就无法正确抓取。

2 个答案:

答案 0 :(得分:2)

您无法为-addURL:方法传递NSData。

如果您的图像存储在磁盘上,则可以使用[UIImage imageWithContentsOfFile:imagePath]方法创建图像。接下来用-addImage添加它:

答案 1 :(得分:0)

 NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/imgtweet.png"];
       [UIImagePNGRepresentation(imageView.image) writeToFile:path atomically:YES];
        //NSLog(@"path %@",path);
        UIImage *new_image = [UIImage imageWithContentsOfFile:path];
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
        {
            SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            [tweetSheet setInitialText:self.txtContent.text];
            [tweetSheet addImage:new_image];
            [self presentViewController:tweetSheet animated:YES completion:nil];
        }

使用以前的答案,谢谢:) 快乐的编码:)