【问题标题】: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 设备。结果相同:空白屏幕截图。
  • 嗨。你找到解决办法了吗?

标签: ios swift xcode sprite-kit


【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      相关资源
      最近更新 更多