发布推特表的截图

时间:2013-05-10 15:42:15

标签: ios twitter

我正在处理我的应用程序添加twitter功能以共享我当前viewcontroller的屏幕截图。我可以使用构建的方法吗?

1 个答案:

答案 0 :(得分:1)

This回答有以下创建屏幕截图的方法:

- (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

它返回一个UIImage,你可以这样使用它:

SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.image = [self captureView:self.view]; // or self.navigationController.view
[self presentViewController:tweetSheet animated:YES completion:^{}];