2012-08-12 26 views
6

我有一个绘图应用程序,我希望我的用户能够使用粒子效果作为其绘图的一部分。基本上,应用程序的重点是执行自定义绘图并保存到相机胶卷或共享通过万维网。当超级层的-renderInContext被调用时,CAEmitterLayer不渲染

我最近上了CAEmitterLayer这个课,我认为这是一种简单而有效的添加粒子效果的方法。

我已经能够在应用程序中使用CAEmitterLayer实现在屏幕上绘制粒子。所以在屏幕上渲染效果很好。

当我去使用

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. 

我觉得渲染图形的内容正在发生

CAEmitterLayer在“动画”粒子的恒定状态。这就是为什么当我尝试渲染图层时(我也尝试渲染layers.presentationLayer和modelLayer),动画永远不会被提交,因此脱离屏幕图像渲染不包含粒子。

问题 有没有人在屏幕外呈现CAEmitterLayer的内容?如果是这样,你是怎么做到的?

替代问题 有谁知道,请不要使用OpenGL和没有任何的cocos2d粒子特效系统库的?

+0

这样的运气呢? – 2012-09-26 17:04:24

+0

不,请参阅下面的Revis答案。 CAEmitterLayer无法调用renderInContext: – micksabox 2013-03-05 16:05:48

回答

4

-[CALayer renderInContext:]在几个简单情况下很有用,但在更复杂的情况下无法按预期工作。您需要找到其他方式来完成您的绘图。

The documentation for -[CALayer renderInContext:]说:

Mac的这种方法的OS X v10.5中执行不 支持整个Core Animation的组合模型。 QCCompositionLayer,CAOpenGLLayer和QTMovieLayer图层不是 呈现。此外,使用3D变换的图层不是 渲染,也不是指定backgroundFilters,滤镜, compositingFilter或蒙版值的图层。未来版本的Mac OS X可能支持 渲染这些图层和属性。

(这些限制适用于iOS的了。)

CALayer.h还说:

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

很好地陈述!其他人想要这样做,有一些没有文档的方法来获取像素,但当然你永远不能在提交的应用程序中使用它。我想知道你是否可以以某种方式在位图上下文中添加一个类似的图层,然后抓住它?这不会是用户看到的,但也许你可以得到类似的东西? – 2012-08-13 00:26:34

+0

你知道任何没有记录的方式吗? – Chris 2015-02-06 20:19:18

0

我能得到我的CAEmitterLayer正确当前动画状态下的电流范围内呈现为图像与

Swift 

func drawViewHierarchyInRect(_ rect: CGRect, 
      afterScreenUpdates afterUpdates: Bool) -> Bool 



Objective-C 

- (BOOL)drawViewHierarchyInRect:(CGRect)rect 
      afterScreenUpdates:(BOOL)afterUpdates 

UIGraphicsBeginImageContextWithOptions(size, false, 0) 

并将afterScreenUpdates设置为true |是

祝你好运与那一个:D

+1

我们可以使用这个,但是CAEmitterLayer会在第二个屏幕截图中变慢。 – Uttam 2017-03-20 06:41:18