嗨,我正在制作一个游戏,并且在游戏中添加了一个共享按钮。我希望用户能够在一条消息中彼此共享一条消息,URL和屏幕快照。它的共享方面工作正常,并且一切正常,但屏幕快照本身显示为空白。这是我用来截屏的代码:
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技术。我是编码新手,所以请非常清楚。非常感谢你!
更新: Xcode 8.2.1•Swift 3.0.2
您需要在游戏场景中添加导入语句和此扩展:
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