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