如何以编程方式拍摄屏幕快照(Swift,SpriteKit)

问题描述:

我尝试了所建议的内容,但输出为白色的空白屏幕截图.这使我假设我没有在视图中添加任何内容.这是我在视图中添加图形的方式. SpriteKit附带了addChild方法,它包含SKSpriteNodes:

I tried what ever was suggested but the output was a white,blank screenshot. Which leads me to assume that I haven't added anything to the view. Here's how I'm adding graphics to my view. The addChild method comes with the SpriteKit and it takes in SKSpriteNodes:

  addChild(background)
    addChild(rate)
    addChild(scoreLabel)
    addChild(share)
    addChild(playAgain)
    addChild(highScoreLabel)
    addChild(scoreBackground)
    addChild(highScoreBackground)

以下是截取屏幕截图的方法:

Here's the method that takes the screenshot:

    UIGraphicsBeginImageContext(self.view!.bounds.size)
    self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

任何建议都会有所帮助

我认为这个问题可以与这个问题合并 迅速在Iphone上截屏只有白色背景

I think this question could be merged with this one Screenshotting on Iphone in swift only has a white background

看起来一样

我想我找到了解决方案. 首先阅读这篇文章:如何拍摄UIView的屏幕截图?

I think I found a solution. First read this post: How Do I Take a Screen Shot of a UIView?

我创建一个Extensions.swift文件,在其中我使用该链接扩展"了UIView的方法.

I create an Extensions.swift file in which I 'extended' the methods of a UIView using that link.

之后,我只需将以下内容连接到我的sprite-kit按钮

After that I simply connect the following to my sprite-kit button

let screenshot = self.view?.pb_takeSnapshot()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

Voilà,图像在相机胶卷中!

Voilà, the image is in the camera roll!