分享我在Facebook的应用程序的屏幕截图(cocos2D 3.0)

时间:2015-05-20 15:00:10

标签: ios facebook cocos2d-iphone

我需要在Facebook上分享我的应用程序的屏幕截图(我使用Cocos2D 3.0),但代码不起作用:

// For take Screenshot of App

-(UIImage *)capture
{
    AppDelegate *_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        // for retina-display
        UIGraphicsBeginImageContextWithOptions(_appDelegate.window.bounds.size, NO, [UIScreen mainScreen].scale);
        [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
    } else {
        // non-retina-display
        UIGraphicsBeginImageContext(_appDelegate.window.bounds.size);
        [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


-(void)btnSharedFacebookTapped:(id)sender
{

[self capture];

    FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
    photo.image = [self capture];
    // photo.userGenerated = YES;
    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];

    [FBSDKShareDialog showFromViewController:self withContent:content delegate:self];
}

1 个答案:

答案 0 :(得分:0)

-(UIImage*) takeGameScreenshot
{
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize size = [[CCDirector sharedDirector] viewSize];
    CCRenderTexture *renderTxture = [CCRenderTexture renderTextureWithWidth:size.width
                                                                  height:size.height];
    [renderTxture begin];
    [[[CCDirector sharedDirector] runningScene] visit];
    [renderTxture end];

    return [renderTxture getUIImage];
}

-(void)facebookShare:(UIImage*)inImage
{
    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    NSString *textShare;

    textShare = [NSString stringWithFormat:@"Checkout ultimate iPhone game %@ - %@", GAME_NAME, GAME_SHORT_LINK];
    [controller setInitialText:textShare];
    [controller addImage:inImage];

    [self.navController presentViewController:controller animated:YES completion:nil];
}

-(void)shareImage
{
    UIImage *resultImage = [self takeGameScreenshot];

    [self facebookShare:mResultImage];
}