ios - 路径在 UIGraphicsBeginImageContextWithOptions 下不可见 - SpriteKit

标签 ios objective-c sprite-kit

我正在我的 SKScene 中绘制一条路径以显示我的对象的轨迹。 一切正常,我可以绘制所有点的路径并将其添加到场景中。 当我尝试打印我的场景时,问题发生了,除了路径之外的所有内容都出现了。 我哪里做错了?

- (void)drawPathTrackBall
{
    // length of my nsmutablearray with the object track
    NSUInteger len = [_trackBallPath count];

    // start image context
    CGRect bounds = self.scene.view.bounds;
    UIGraphicsBeginImageContextWithOptions(bounds.size, YES, [UIScreen mainScreen].scale);
    [self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];

    // draw my path
    SKShapeNode *line = [[SKShapeNode alloc] init];
    CGMutablePathRef linePath = CGPathCreateMutable();
    CGPathMoveToPoint(linePath, NULL, _ballStartX, _ballStartY);

    for(NSUInteger i=0; i<len; ++i)
    {
        NSValue *nsPoint = [_trackBallPath objectAtIndex:i];
        CGPoint p = nsPoint.CGPointValue;
        CGPathAddLineToPoint(linePath, NULL, p.x, p.y);
    }

    line.path = linePath;
    line.lineWidth = 0.5f;
    line.strokeColor = [UIColor redColor];

    [_container addChild:line];

    _screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

谢谢

最佳答案

首先 - 您正在创建简单的路径,它没有连接或绘制到您代码中的某个地方到上下文。我还没有学过 SpriteKit,但我很了解 CoreGraphics,我认为你不能简单地使用那里的 SpriteKit API 来创建屏幕截图。而是尝试使用 native CoreGraphics 方法。

要使您的代码正常工作,您需要:

  • 获取您将绘制的上下文。

  • 因为您已经创建了路径 - 只需将其添加到上下文即可。

  • 然后描边/填充它。

要获取绘制调用的当前上下文:CGContextRef ctx = UIGraphicsGetCurrentContext();

添加路径使用:CGContextAddPath(ctx, path);

描边/填充使用 CGContextStrokePath(ctx);/CGContextFillPath(ctx);

所以你更新后的代码应该是这样的(注意我已经删除了与 SpriteKit 相关的代码):

- (void)drawPathTrackBall
{
    // length of my nsmutablearray with the object track
    NSUInteger len = [_trackBallPath count];

    // start image context
    CGRect bounds = self.scene.view.bounds;
    UIGraphicsBeginImageContextWithOptions(bounds.size, YES, [UIScreen mainScreen].scale);
    [self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];

    CGContextRef ctx = UIGraphicsGetCurrentContext(); // Obtain context to draw.
    CGMutablePathRef linePath = CGPathCreateMutable();
    CGPathMoveToPoint(linePath, NULL, _ballStartX, _ballStartY);

    for(NSUInteger i=0; i<len; ++i)
    {
        NSValue *nsPoint = [_trackBallPath objectAtIndex:i];
        CGPoint p = nsPoint.CGPointValue;
        CGPathAddLineToPoint(linePath, NULL, p.x, p.y);
    }

    // I've exchanged SpriteKit node API calls with CoreGraphics equivalents.
    CGContextSetLineWidth(ctx, 0.5f); 
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextAddPath(ctx, linePath);
    CGContextStrokePath(ctx);

    _screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

试一试。

关于ios - 路径在 UIGraphicsBeginImageContextWithOptions 下不可见 - SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915503/

相关文章:

iphone - 如何检查哪个选项卡栏项目处于事件状态?

iphone - 迁移到识别设备的新方法

objective-c - 以编程方式启动 OSX 中的默认程序

ios - 当 SceneKit 动画停止时,SpriteKit 的动画覆盖在 SceneKit 上停止

objective-c - 来自 SKTexture 的 UIImage

ios - 在 Swift 中的 View Controller 之间传递数据

ios - imageAvailableCallback 从未在基本 GPUImage2 相机设置中调用

objective-c - 以编程方式将击键发送到 OS X

objective-c - Setter方法很奇怪,有人可以解释一下

ios - 使用函数更新其他类中的标签