自动获取游戏截图并上传到Facebook

时间:2014-05-12 14:28:24

标签: ios objective-c facebook twitter

我想添加一个功能,自动将游戏中的提醒屏幕截图作为证据,并将其上传到Facebook或Twitter,而不将截屏保存到照片库。如何将其集成到我的代码中?

BTW:我已经搜索了这个主题,我确实得到了可能对此有益的结果,但我只是在学习开发iOS应用程序,如果你能给我一些详细的帮助,我会很高兴...:)

*对不起,我来自德国,所以这里有一些翻译:
“KlickMich” - > “TapMe”
“Zeit” - > “时代”
“Punktestand” - > “点”
“Die Zeit ist um” - > “时间到了”
“Du hast 11 Punkte erzielt” - > “你得了11分” “Auf Facebook teilen” - > “在Facebook上分享”
“Auf Twitter teilen” - > “在Twitter上分享”
“Nochmal spielen” - > “再玩一次”
“Habe gerade 0 Punkte in KlickMich erreicht ......” - > “只是在TapMe中得了0分......”

截图 - > http://techstern.de/app/screenshot1.png
XCode项目 - > http://techstern.de/app/KlickMich.zip


代码:ViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.m"

@interface ViewController : UIViewController<UIAlertViewDelegate> {
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;

NSInteger count;
NSInteger seconds;
NSTimer *timer;

int time;
}

- (IBAction) buttonPressed;
- (void)setupGame;
- (void)subtractTime;
- (IBAction)postToTwitter;
- (IBAction)postToFacebook;

@end


代码:ViewController.m

#import "ViewController.h"
#import <UIKit/UIAlertView.h>
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)buttonPressed{
count++;

scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

}

- (void)setupGame{
seconds = 15;
count = 0;

timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];
scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                         target:self
                                       selector:@selector(subtractTime)
                                       userInfo:nil
                                        repeats:YES];

}

- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];

if (seconds == 0) {
    [timer invalidate];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Die Zeit ist um"
                                                    message:[NSString       stringWithFormat:@"Du hast %li Punkte erzielt",(long)count]
                                                   delegate:self
                                          cancelButtonTitle:@"Nochmal spielen"
                                          otherButtonTitles:@"Auf Facebook teilen", @"Auf Twitter teilen", nil];

    [alert show];
}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 1)
{
    [self postToTwitter];
}
else if (buttonIndex == 2)
{
    [self postToFacebook];
}
    [self setupGame];
}

- (IBAction)postToTwitter{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}
} 

- (IBAction)postToFacebook{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [controller setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [self presentViewController:controller animated:YES completion:Nil];
}
}

@end

1 个答案:

答案 0 :(得分:0)

这是我在当前游戏中使用的代码。

UIImage *sharingUIImageFromResults(NSString* imageFile, long score, Level* level)
{
    GameOverSharingView *sharingView = [[UINib nibWithNibName:@"GameOverSharingView"
                                                       bundle:[NSBundle mainBundle]] instantiateWithOwner:nil
                                        options:nil][0];
    sharingView.imageView.image = [UIImage imageWithContentsOfFile:imageFile];

    sharingView.gameTitleLabel.text = ...;
    sharingView.scoreLabel.text = ...;
    sharingView.challengeLabel.text = ...;

    UIGraphicsBeginImageContext(sharingView.frame.size);
    [sharingView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

你会看到我根据游戏中发生的事情生成一个单独的视图。你可以直接拿走当前视图并致电renderInContext:,这取决于你。

修改

以下是要添加到UIViewController课程的代码:

- (UIImage *)screenshot
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

然后您的postToTwitter方法可以更改为:

- (IBAction)postToTwitter{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet addImage:[self screenshot]];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %ld Punkte in KlickMich erreicht...",(long)count]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}