2017-06-17 85 views
-1

我想截取任何正在運行的應用程序的截圖,而我的應用程序在後臺。關閉應用程序時截圖

我在applicationDidEnterBackground方法中設置了一個計時器。

func applicationDidEnterBackground(_ application: UIApplication) { 
    var timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.takeSS), userInfo: nil, repeats: true) 
} 

我創建了一個名爲takeSS

func takeSS(){ 

    UIGraphicsBeginImageContextWithOptions((window?.bounds.size)!, false, UIScreen.main.scale) 

    self.window?.drawHierarchy(in: (window?.bounds)!, afterScreenUpdates: true) 
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 


    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 

} 

它需要每10秒拍攝畫面並將其保存到相機膠捲方法。但截圖是白色和空白的照片。當應用程序關閉時,是否有任何方法可以進行屏幕拍攝?

謝謝。

+0

只有幾種類型的後臺處理允許 - https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/ TP40007072-CH4-SW4 - 這不是其中之一。你基本上要求一個沙盒應用程序可以窺探其他沙盒應用程序。 – Ssswift

回答

3

你不能那樣做。出於安全原因,Apple屏蔽了您需要截取iOS 9以外應用程序屏幕截圖的API。我爲我工作過的一家公司研究過這個問題,發現除非設備被越獄,否則這是不可能的。