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

查看:25
本文介绍了屏幕截图显示为空白 - 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天全站免登陆