0

我正在开发我的应用程序以添加 twitter 功能以共享我当前视图控制器的屏幕截图。有没有我可以使用的内置方法?

4

1 回答 1

1

答案具有以下创建屏幕截图的方法:

- (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:^{}];
于 2013-05-10T15:48:46.820 回答