Spritekit 截屏功能

SpriteKit 不是 UIKit 可以直接使用 UIView 的 layer 来读取渲染上下文,而是通过一些方法转换才能得到。

方法一:

-(UIImage *)imageFromNode:(SKNode *)node {

         SKView *view = node.scene.view;

        CGFloat scale = [UIScreen mainScreen].scale;

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);

         [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];

        UIImage *sceneSnapshot =    UIGraphicsGetImageFromCurrentImageContext();

       UIGraphicsEndImageContext();

        return sceneSnapshot;

}

方法二:

- (UIImage*) imageWithView:(UIView *)view {

       UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);

      [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];

      UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

      UIGraphicsEndImageContext();

     return img;

}

- (UIImage*) imageFromNode:(SKNode*)node

{

        SKTexture*      tex    = [self.scene.view textureFromNode:node];

        SKView*        view    = [[SKView alloc]initWithFrame:CGRectMake(0, 0,      tex.size.width, tex.size.height)];

        SKScene*        scene  = [SKScene sceneWithSize:tex.size];

        SKSpriteNode*  sprite  = [SKSpriteNode spriteNodeWithTexture:tex];

        sprite.position = CGPointMake( CGRectGetMidX(view.frame),        CGRectGetMidY(view.frame) );

         [scene addChild:sprite];

        [view presentScene:scene];

        return [self imageWithView:view];

}

你可能感兴趣的:(Spritekit 截屏功能)