質問

I would like to add a function which takes automatically a screenshot of the alert in the game as a proof and upload it to Facebook or Twitter without saving the taken screenshot to the photo library. How can I integrate this into my code?

BTW: I already googled this topic and I did get results that could be good for this, but I´m just learning to develop iOS Apps and I would love it if you could give me some detailed help… :)

*Sorry, I´m from german, so here's some translation:
"KlickMich" -> "TapMe"
"Zeit" -> "Time"
"Punktestand" -> "Points"
"Die Zeit ist um" -> "Time is up"
"Du hast 11 Punkte erzielt" -> "You scored 11 Points"
"Auf Facebook teilen" -> "Share on Facebook"
"Auf Twitter teilen" -> "Share on Twitter"
"Nochmal spielen" -> "Play again"
"Habe gerade 0 Punkte in KlickMich erreicht..." -> "Just scored 0 points in TapMe..."!

Screenshot -> http://techstern.de/app/screenshot1.png
XCode Project -> http://techstern.de/app/KlickMich.zip


Code: 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


Code: 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
役に立ちましたか?

解決

Here's the code I'm using in my current game.

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;
}

You'll see that I generate a separate view from what's happening in the game. You can probably just take your current view and call renderInContext:, it's up to you.

EDIT

Here's code to add to your UIViewController class:

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

Then your postToTwitter method can be changed to this:

- (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];
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top