从Xcode for Mac中的SpriteKit Node / SpriteNode创建png图像

时间:2015-08-30 18:49:35

标签: image macos swift sprite-kit

目前,我正在制作等距地图编辑器(在Swift中),其中每个地图图块都添加到名为SKNode的{​​{1}}。我想知道在完成地图设计后,是否可以从mapLayer生成png图像?

3 个答案:

答案 0 :(得分:1)

不是在Swift中,但它应该让你知道如何做到这一点。

您可以通过以下方式将屏幕捕获到UIImage:

CGRect bounds = self.scene.view.bounds;
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage* screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

然后从UIImage创建一个PNG图像(并将其写入磁盘),如下所示:

// Create paths to output images
NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

来源here

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

// Let's check to see if files were successfully written...

// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

来源here

答案 1 :(得分:0)

这是一个如何将视图内容写入PNG文件的示例。首先,定义一个UIView扩展,捕获视图的内容并将其转换为UIImage

extension UIView {
    func screenGrab() -> UIImage {
        // Uncomment this (and comment out the next statement) for retina screen capture
        // UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1.0)
        drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

然后,定义一个将UIImage写入文件的UIImage扩展名。

extension UIImage {
    func writeToFile(fileName:String) {
        let path = NSHomeDirectory().stringByAppendingPathComponent("Documents/"+fileName)
        UIImagePNGRepresentation(self).writeToFile(path, atomically: true)
    }
}

最后,使用

捕获视图内容并将其写入PNG文件
self.view?.screenGrab().writeToFile("capture.png")

以及如何确定PNG的存储位置:

println(NSHomeDirectory().stringByAppendingPathComponent("Documents"))

答案 2 :(得分:0)

这是macOS和iOS行为差异很大的实例之一。剧透:将NSView内容捕获到NSImage的常用方法不起作用。

NSImage(data:inside :)和NSView.bitmapImageRepForCachingDisplay(in:to)可以很好地用于NSView,但是如果您嵌入SKView或直接将其应用于SKView,则只会得到一块空白画布。

解决方案在于思考SpriteKit:

SKScene本质上是一个SKNode(实际上是一个SKEmitterNode),而SKView具有一个称为texture(来自:SKNode)的方法,该方法为您提供SKTexture。 SKTexture具有cgImage()属性,就在这里。

这在呈现场景的NSViewController中假定了三个属性:skView:SKView,scene:SKScene?和用于显示的NSImageView,名为snapView。

  if  let rendered = skView.texture(from: scene!){
        snapView.image = NSImage(cgImage: rendered.cgImage(), size: self.skView.bounds.size)

(不要强行打开包装,仅用于演示目的)