如何以编程方式截屏(Swift、SpriteKit) [英] How to take screen shot programmatically (Swift, SpriteKit)

查看:24
本文介绍了如何以编程方式截屏(Swift、SpriteKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了任何建议,但输出是白色的空白屏幕截图.这使我假设我没有在视图中添加任何内容.这是我如何将图形添加到我的视图中.addChild 方法是 SpriteKit 自带的,它接受 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)

截图方法如下:

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

任何建议都会有所帮助

推荐答案

我觉得这个问题可以和这个问题合并在 swift 中在 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à, the image is in the camera roll!

这篇关于如何以编程方式截屏(Swift、SpriteKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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