截图在模拟器中有效,但在真实设备上失败
Posted
技术标签:
【中文标题】截图在模拟器中有效,但在真实设备上失败【英文标题】:Taking screenshot works in simulator but fails on real device 【发布时间】: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 设备。结果相同:空白屏幕截图。 嗨。你找到解决办法了吗? 【参考方案1】:这是我用来截屏的代码。最大的不同是我对 afterScreenUpdates 的看法是错误的:
public extension UIView
public func snapshotImage() -> UIImage?
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: false)
let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshotImage
public func snapshotView() -> UIView?
if let snapshotImage = snapshotImage()
return UIImageView(image: snapshotImage)
else
return nil
【讨论】:
【参考方案2】:根据 Apple 的回复,出于安全原因,我们无法这样做。 https://developer.apple.com/forums/thread/108278?answerId=613818022#613818022
【讨论】:
以上是关于截图在模拟器中有效,但在真实设备上失败的主要内容,如果未能解决你的问题,请参考以下文章
音频引擎仅在运行 iOS 10 的设备(iphone 6)上失败,在所有模拟器设备上运行良好或真实设备直到 iphone 6s