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方法附带SpriteKit,它接收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)
Any suggestions would be helpful
任何的意见都将会有帮助
2 个解决方案
#1
11
I think this question could be merged with this one Screenshotting on Iphone in swift only has a white background
我认为这个问题可以合并到这个问题。在swift上的Iphone截图只有白色背景
which seems the same
这似乎是一样的
EDIT:
编辑:
I think I found a solution. First read this post: How Do I Take a Screen Shot of a UIView?
我想我找到了解决方案。首先阅读这篇文章:如何拍摄UIView的屏幕截图?
I create an Extensions.swift file in which I 'extended' the methods of a UIView using that link.
我创建了一个Extensions.swift文件,其中我使用该链接扩展了UIView的方法。
After that I simply connect the following to my sprite-kit button
之后,我只需将以下内容连接到我的sprite-kit按钮
let screenshot = self.view?.pb_takeSnapshot()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
Voilà, the image is in the camera roll!
Voilà,图像在相机胶卷中!
#2
1
Here is how I solved it in swift 3: This captures all the nodes in your scene: Note: in below code represents the view it is capturing from. You may want to update it with your view in project.
以下是我在swift 3中解决它的方法:它捕获场景中的所有节点:注意:在下面的代码中表示它正在捕获的视图。您可能希望使用项目中的视图更新它。
func captureScreen() -> UIImage {
UIGraphicsBeginImageContextWithOptions(yourview.bounds.size, true, 0)
yourview.drawHierarchy(in: yourview.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
#1
11
I think this question could be merged with this one Screenshotting on Iphone in swift only has a white background
我认为这个问题可以合并到这个问题。在swift上的Iphone截图只有白色背景
which seems the same
这似乎是一样的
EDIT:
编辑:
I think I found a solution. First read this post: How Do I Take a Screen Shot of a UIView?
我想我找到了解决方案。首先阅读这篇文章:如何拍摄UIView的屏幕截图?
I create an Extensions.swift file in which I 'extended' the methods of a UIView using that link.
我创建了一个Extensions.swift文件,其中我使用该链接扩展了UIView的方法。
After that I simply connect the following to my sprite-kit button
之后,我只需将以下内容连接到我的sprite-kit按钮
let screenshot = self.view?.pb_takeSnapshot()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
Voilà, the image is in the camera roll!
Voilà,图像在相机胶卷中!
#2
1
Here is how I solved it in swift 3: This captures all the nodes in your scene: Note: in below code represents the view it is capturing from. You may want to update it with your view in project.
以下是我在swift 3中解决它的方法:它捕获场景中的所有节点:注意:在下面的代码中表示它正在捕获的视图。您可能希望使用项目中的视图更新它。
func captureScreen() -> UIImage {
UIGraphicsBeginImageContextWithOptions(yourview.bounds.size, true, 0)
yourview.drawHierarchy(in: yourview.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}