ios 屏幕截图显示空白- Swift

vh0rcniy  于 2023-05-30  发布在  iOS
关注(0)|答案(3)|浏览(344)

嗨,我正在制作一个游戏,我已经添加了一个分享按钮到游戏。我希望用户能够共享一条消息,网址,并在一条消息中彼此沿着截图。它的共享方面工作正常,一切都显示出来,除了屏幕截图本身显示为空白。这是我用来截图的代码:

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技术,如果这使一个差异。我是新的编码,所以请非常清楚。非常感谢!

mbjcgjjk

mbjcgjjk1#

更新:Xcode 8.2.1 · Swift 3.0.2
您需要将import语句和这个扩展添加到游戏场景中:

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
tag5nh1u

tag5nh1u2#

在WWDC 2015中,UIVisualEffectView中的新功能,一个解决方案被“掩盖”,用于捕获包含振动标签和模糊的UIVisualEffectView的屏幕截图。经常出现的问题是,苹果的截图API往往不会捕获UIVisualEffect,导致视图的截图没有视觉效果。根据WWDC 2015的解决方案涉及在窗口/屏幕处捕获快照;不幸的是,没有显示示例代码。以下是我自己的实现:

//create snapshot of the blurView
let window = UIApplication.sharedApplication().delegate!.window!!
//capture the entire window into an image
UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.mainScreen().scale)
window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
let windowImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//now position the image x/y away from the top-left corner to get the portion we want
UIGraphicsBeginImageContext(blurView.frame.size)
windowImage.drawAtPoint(CGPoint(x: -blurView.frame.origin.x, y: -blurView.frame.origin.y))
let croppedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
//embed image in an imageView, supports transforms.
let resultImageView = UIImageView(image: croppedImage)

注意:此快照代码仅在调用ViewDidAppear后才有效。

jhiyze9q

jhiyze9q3#

在我的例子中,我在用scandit SDK扫描护照时进行了屏幕截图,结果是一个空白(黑色)图像,然后我从scandit**图像缓冲区中获取图像并使用该图像。
您可能会在应用程序中使用一些相机SDK,这些SDK会阻止屏幕截图并返回空白(黑色)图像。

相关问题