无法创建 SCNView 的屏幕截图 [英] Cannot create a Screenshot of a SCNView

查看:25
本文介绍了无法创建 SCNView 的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取 SCNView 的屏幕截图?我正在尝试使用以下代码,但它总是显示为白色...

Is it possible to get a screenshot of an SCNView? I'm trying with the below code, but it always comes out white...

NSRect bounds = [window.contentView bounds];
NSImage *screenshot = [[NSImage alloc] initWithData:[window.contentView dataWithPDFInsideRect:bounds]];

当视图是标准 NSView 时它工作正常...

It works fine when the view is a standard NSView...

推荐答案

SceneKit 使用 OpenGL 上下文进行绘制.您无法像基于 Quartz 的上下文(如普通"AppKit 视图所使用的那样)那样轻松地将其转换为 PDF 数据.
但是您可以从 OpenGL 中获取光栅化位图数据:

SceneKit uses an OpenGL context to draw. You can't turn that into PDF data as easily as a Quartz based context (as used by "normal" AppKit views).
But you can grab the rasterized bitmap data from OpenGL:

- (IBAction)takeShot:(id)sender
{
    NSString* path = @"/Users/weichsel/Desktop/test.tiff";
    NSImage* image = [self imageFromSceneKitView:self.scene];
    BOOL didWrite = [[image TIFFRepresentation] writeToFile:path atomically:YES];
    NSLog(@"Did write:%d", didWrite);
}

- (NSImage*)imageFromSceneKitView:(SCNView*)sceneKitView
{
    NSInteger width = sceneKitView.bounds.size.width * self.scene.window.backingScaleFactor;
    NSInteger height = sceneKitView.bounds.size.height * self.scene.window.backingScaleFactor;
    NSBitmapImageRep* imageRep=[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                                       pixelsWide:width
                                                                       pixelsHigh:height
                                                                    bitsPerSample:8
                                                                  samplesPerPixel:4
                                                                         hasAlpha:YES
                                                                         isPlanar:NO
                                                                   colorSpaceName:NSCalibratedRGBColorSpace
                                                                      bytesPerRow:width*4
                                                                     bitsPerPixel:4*8];
    [[sceneKitView openGLContext] makeCurrentContext];
    glReadPixels(0, 0, (int)width, (int)height, GL_RGBA, GL_UNSIGNED_BYTE, [imageRep bitmapData]);
    [NSOpenGLContext clearCurrentContext];
    NSImage* outputImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
    [outputImage addRepresentation:imageRep];
    NSImage* flippedImage = [NSImage imageWithSize:NSMakeSize(width, height) flipped:YES drawingHandler:^BOOL(NSRect dstRect) {
        [imageRep drawInRect:dstRect];
        return YES;
    }];
    return flippedImage;
}

不要忘记链接 OpenGL.framework 和 #import "OpenGL/gl.h"

Don't forget to link OpenGL.framework and #import "OpenGL/gl.h"

更新
SceneKit 似乎使用翻转上下文.我添加了一些代码来修复颠倒的图像.

Update
SceneKit seems to use a flipped context. I added some code to fix the upside-down image.

更新 2
更新代码以考虑支持比例因子(对于视网膜显示器)

Update 2
Updated code to take the backing scale factor into account (for retina displays)

这篇关于无法创建 SCNView 的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆