kotowo
3/10/2016 - 8:06 AM

screen capture

screen capture

func captureScreenAndSave() {

    func captureScreen()->UIImage {
        let sc = UIScreen.mainScreen()
        UIGraphicsBeginImageContextWithOptions(sc.bounds.size, true, sc.scale)
        let contextRef = UIGraphicsGetCurrentContext()!
        for window:UIWindow in UIApplication.sharedApplication().windows {
            window.layer.renderInContext(contextRef)
        }
        let captured = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return captured
    }
    
    let image = captureScreen()
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)) , nil)
}

// UIImageWriteToSavedPhotosAlbum() completion handler
func image(image: UIImage, didFinishSavingWithError error: NSError!, contextInfo: UnsafeMutablePointer<Void>) {
    if error != nil {
        print(error.code)
    }
}