CAEmitterLayer does not display when calling -renderInContext: superlayer

I have a drawing application and I would like my users to be able to use particle effects as part of their drawing. Basically, the point of the application is to execute a custom drawing and save it to Camera Roll or transfer it over the World Wide Web.

I recently discovered the CAEmitterLayer class, which I believe would be a simple and efficient way to add particle effects.

I was able to draw particles on the screen in the application using the CAEmitterLayer implementation. Thus, rendering on the screen works great.

When I talk about the contents of a picture with

 CGContextRef context = UIGraphicsBeginImageContextWithSize(self.bounds.size); // The instance drawingView has a CAEmitterLayer instance in its layer/view hierarchy [drawingView.layer renderInContext:context]; //Note: I have also tried using the layer.presentationLayer and still nada .... //Get the image from the current image context here for saving to Camera Roll or sharing ....the particles are never rendered in the image. 

What I think is happening

CAEmitterLayer is in a constant state of "revitalization" of particles. Therefore, when I try to make a layer (I also tried visualizing layers.presentationLayer and modelLayer), the animations never run, so rendering the image without a screen does not contain particles.

Question Has anyone displayed the contents of CAEmitterLayer off-screen? If so, how did you do this?

Alternative question Does anyone know of any system effect libraries that do not use OpenGL and are not Cocos2D?

+6
source share
2 answers

-[CALayer renderInContext:] is useful in a few simple cases, but will not work as expected in more complex situations. You will need to find another way to make your drawing.

The documentation for -[CALayer renderInContext:] states:

Implementing this method on Mac OS X v10.5 does not support the entire Core Animation composition model. QCCompositionLayer, CAOpenGLLayer and QTMovieLayer levels are not rendered. In addition, layers that use 3D transforms are not and also layers that define backgroundFilters, filters, compositingFilter, or mask values. Future versions of Mac OS X may add support for rendering these layers and properties.

(These restrictions also apply to iOS.)

The CALayer.h header also says:

  * WARNING: currently this method does not implement the full * CoreAnimation composition model, use with caution. */ 
+4
source

I was able to get my CAEmitterLayer, which was displayed as an image correctly in the current animation state using

 Swift func drawViewHierarchyInRect(_ rect: CGRect, afterScreenUpdates afterUpdates: Bool) -> Bool Objective-C - (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates 

in the current context

 UIGraphicsBeginImageContextWithOptions(size, false, 0) 

and set afterScreenUpdates to true | Yes

Good luck with that: D

0
source

Source: https://habr.com/ru/post/922691/


All Articles