iPhone接听电话时如何截图

时间:2019-01-19 08:57:29

标签: ios iphone swift xcode10 callkit

我要管理来电操作以截屏。进行了屏幕截图,但是没有我的来电屏幕。我该如何实现?

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var callObserver: CXCallObserver!
var photoDelegate: UIImageView!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    callObserver = CXCallObserver()
    callObserver.setDelegate(self, queue: nil) // nil queue means main thread
    return true
}
}

还有CXCallObserverDelegate

extension AppDelegate: CXCallObserverDelegate {
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
    if call.hasEnded == true {

    }
    if call.isOutgoing == true && call.hasConnected == false {      
    }
    if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
        print("Incoming \(call.uuid.description)")
 imagine = UIImageView(image: UIApplication.shared.screenShot!)
    }

    if call.hasConnected == true && call.hasEnded == false {

  }
}
}

屏幕截图

extension UIApplication {

var screenShot: UIImage?  {

    if let rootViewController = keyWindow?.rootViewController {
        let scale = UIScreen.main.scale
        let bounds = rootViewController.view.bounds
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale);
        if let _ = UIGraphicsGetCurrentContext() {
            rootViewController.view.drawHierarchy(in: bounds, afterScreenUpdates: true)
            let screenshot = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return screenshot
        }
    }
    return nil
}
}

1 个答案:

答案 0 :(得分:0)

用户接听电话时显示的CallKit UI是iOS的一部分,而不是您的应用程序的一部分,在接听电话后,该应用程序将成为焦点,您可以获取屏幕截图。

Replaykit是替代解决方案。