gpt4 book ai didi

ios - 截图在模拟器中有效,但在真实设备上失败

转载 作者:搜寻专家 更新时间:2023-10-31 23:09:55 25 4
gpt4 key购买 nike

我有以下代码截取游戏屏幕截图,然后在点击“分享”按钮时分享它。

它在模拟器上运行良好并返回正确的图像。但是,在我的真实设备上,它返回一个白色图像。每次。

代码如下:

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 应用程序没有任何更改。它们返回相同的结果:空白图像。

感谢所有帮助!

最佳答案

这是我用来截图的代码。最大的不同是我有 false,而你有 true 的 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
}
}
}

关于ios - 截图在模拟器中有效,但在真实设备上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43945165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com