【发布时间】:2017-05-12 19:29:17
【问题描述】:
我有以下代码截取游戏屏幕截图,然后在点击“分享”按钮时分享它。
它在模拟器上运行良好并返回正确的图像。但是,在我的真实设备上,它返回一个白色图像。每次。
代码如下:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let Touch : UITouch! = touches.first
let TouchLocation = Touch.location(in: self)
if let body = self.physicsWorld.body(at: TouchLocation){
if body.node!.name == "shareButton" {
let postText: String = "Check out my score! Can you beat it?"
let postImage: UIImage = getScreenshot()
let activityItems = [postText as AnyObject, postImage] //as [Any]
let activityController = UIActivityViewController(
activityItems: activityItems,
applicationActivities: nil
)
let controller: UIViewController = scene!.view!.window!.rootViewController!
controller.present(
activityController,
animated: true,
completion: nil
)
}
}
}
函数如下:
func getScreenshot() -> UIImage {
let snapshotView = self.view!.snapshotView(afterScreenUpdates: true)
let bounds = UIScreen.main.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
snapshotView?.drawHierarchy(in: bounds, afterScreenUpdates: true)
let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return screenshotImage;
}
我将问题缩小到这几行代码:
let postText: String = "Check out my score! Can you beat it?"
let postImage: UIImage = getScreenshot()
let activityItems = [postText as AnyObject, postImage] //as [Any]
let activityController = UIActivityViewController(
activityItems: activityItems,
applicationActivities: nil
)
let controller: UIViewController = scene!.view!.window!.rootViewController!
controller.present(
activityController,
animated: true,
completion: nil
)
func getScreenshot() -> UIImage {
let snapshotView = self.view!.snapshotView(afterScreenUpdates: true)
let bounds = UIScreen.main.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
snapshotView?.drawHierarchy(in: bounds, afterScreenUpdates: true)
let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return screenshotImage;
}
请注意,我只使用一个类。这个:
class GameScene: SKScene {
}
我通过创建一个新应用来缩小范围。除了添加这些代码行之外,我没有对默认的 Hello World 应用程序进行任何更改。它们返回相同的结果:空白图像。
感谢所有帮助!
【问题讨论】:
-
sim和设备的ios版本一样吗?
-
Sim 是 10.3,Device 是 10.3.1
-
这很令人困惑......我也遇到过类似的问题,但只有当设备比 sim 旧或新时。我唯一建议的是创建新的 xcode 项目,然后尝试第二个设备
-
尝试了 10.0 设备。结果相同:空白屏幕截图。
-
嗨。你找到解决办法了吗?
标签: ios swift xcode sprite-kit