屏幕截图显示空白 - Swift [英] Screenshot showing up blank - Swift

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

问题描述

我正在制作一款游戏,并为游戏添加了一个分享按钮。我希望用户能够在一条消息中彼此共享消息,URL和屏幕截图。它的共享方面工作正常,一切都在显示,除了屏幕截图本身显示为空白。这是我用来截取屏幕截图的代码:

Hi I am making a game and I have added a share button to the game. I want the user to be able to share a message, URL, and screenshot along side each other in one message.The sharing aspect of it is working fine and everything is showing up except that the screenshot itself is showing up blank. This is the code I am using to take a screenshot:

   let layer = UIApplication.sharedApplication().keyWindow!.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

    layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

    println("screenshot")

请帮我解决这个问题请务必使用Swift语言。我也在使用SpriteKit技术,如果这有所作为。我是编码的新手,所以请非常清楚。非常感谢!

Please help me resolve this issue and please be sure to do so in the Swift language. Also I am using SpriteKit Technology if that makes a difference. I am new to coding so please be very clear. Thank you very much!

推荐答案

更新: Xcode 8.2.1•Swift 3.0.2

您需要将import语句和此扩展名添加到游戏场景中:

You need to add the import statement and this extension to your game scene:

import UIKit 







extension UIView {
    var snapshot: UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
        defer { UIGraphicsEndImageContext() }
        drawHierarchy(in: bounds, afterScreenUpdates: true)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}







let myImage = view?.snapshot

这篇关于屏幕截图显示空白 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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