2012-08-12 30 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