swiftcaemitterlayer

Screenshot View with CAEmitterLayer


Is it possible to take a screenshot of a view that includes a CAEmitterLayer?

Whenever I try, the view is created fine, but all of the particles are missing, here's my code:

UIGraphicsBeginImageContext(drawingView.frame.size)
var context:CGContextRef = UIGraphicsGetCurrentContext()
drawingView.layer.renderInContext(context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image

I have confirmed that the CAEmitterLayer is on the drawingView.layer


Solution

  • This works for iOS, but now I need a similar method for OSX:

    func offscreenScreenshot(drawingView:UIView) -> Image
    {
        UIGraphicsBeginImageContextWithOptions(drawingView.frame.size, true, 1.0)
        let res = drawingView.drawViewHierarchyInRect(drawingView.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }