1

我正在尝试覆盖在UIView类中实现的粒子发射器ParticleEmitter

当我尝试将其添加UIView为 auiElementInput时,它不会显示。相机输入仍然有效,但粒子发射器不显示。

更新代码:

//ParticleEmitter source:

//=========================================================
//  ParticleEmitter.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ParticleEmitter : UIView{
 CAEmitterLayer *emitter;
}
@end

//=========================================================
//  ParticleEmitter.m

#import "ParticleEmitter.h"

@implementation ParticleEmitter

- (id)initWithFrame:(CGRect)frame

{
    self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    float multiplier = 0.25f;

     CGPoint pt;
     pt.x = (frame.origin.x+(frame.size.width/2));
     pt.y = (frame.origin.y+frame.size.height/2);

     //Create the emitter layer
     emitter = [CAEmitterLayer layer];
     emitter.emitterPosition = pt;
     emitter.emitterMode = kCAEmitterLayerOutline;
     emitter.emitterShape = kCAEmitterLayerCircle;
     emitter.renderMode = kCAEmitterLayerAdditive;
     emitter.emitterSize = CGSizeMake(100 * multiplier, 0);

     //Create the emitter cell
     CAEmitterCell* particle = [CAEmitterCell emitterCell];
     particle.scale=0.05;
     particle.emissionLongitude = M_PI;
     particle.birthRate = multiplier * 100.0;
     particle.lifetime = multiplier*30;
     particle.lifetimeRange = multiplier * 4.0f;
     particle.velocity = 300;
     particle.velocityRange = 400;
     particle.emissionRange = 5.5;
     particle.scaleSpeed = 0.05; // was 0.3
     particle.alphaRange = 0.02;
     particle.alphaSpeed = 0.5;



     //particle.color = [[COOKBOOK_PURPLE_COLOR colorWithAlphaComponent:0.5f] CGColor];
     particle.contents = (__bridge id)([UIImage imageNamed:@"baloon.png"].CGImage);
     particle.name = @"particle";

     emitter.emitterCells = [NSArray arrayWithObject:particle];
     [self.layer addSublayer:emitter];


     [CATransaction begin];
     [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
     // emitter.emitterPosition = pt;
     [CATransaction commit];



      }


return self;

}

//================================================= =========

//我想如何将粒子发射器与GPUimage一起使用:

         newfilter = [[GPUImageSepiaFilter alloc] init];

        blendFilter = nil;


        blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
        blendFilter.mix = 1.0;


        CGRect pviewFrame = CGRectMake(0, 0, 640, 480 );
        UIView *pView = [[ParticleEmitter alloc] initWithFrame:pviewFrame];


        uiElementInput = [[GPUImageUIElement alloc] initWithView:pView];


        [newfilter  addTarget:blendFilter];
        [uiElementInput addTarget:blendFilter];



        __unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

        [newfilter  setFrameProcessingCompletionBlock:^(GPUImageOutput *newfilter, CMTime frameTime){
            pView.alpha = 0.9;
                [weakUIElementInput update];
        }];


        [newfilter addTarget:filterView];
        [videoCamera addTarget:newfilter];

//================================================= ========= //FilterShowcase 中适用于文本的示例代码:

        newfilter = [[GPUImageSepiaFilter alloc] init];

        blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
        blendFilter.mix = 1.0;

        NSDate *startTime = [NSDate date];

        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 640.0f, 480.0f)];
        timeLabel.font = [UIFont systemFontOfSize:17.0f];
        timeLabel.text = @"Time: 0.0 s";
        timeLabel.textAlignment = UITextAlignmentCenter;
        timeLabel.backgroundColor = [UIColor clearColor];
        timeLabel.textColor = [UIColor whiteColor];

        uiElementInput = [[GPUImageUIElement alloc] initWithView:timeLabel];

        [newfilter  addTarget:blendFilter];
        [uiElementInput addTarget:blendFilter];


        __unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

        [newfilter  setFrameProcessingCompletionBlock:^(GPUImageOutput * newfilter, CMTime frameTime){
            timeLabel.text = [NSString stringWithFormat:@"Time: %f s", -[startTime timeIntervalSinceNow]];
            [weakUIElementInput update];
        }];

         [newfilter addTarget:filterView];

         [blendFilter addTarget:filterView];

         [videoCamera addTarget:newfilter];

//================================================= =========

有什么建议么?

4

1 回答 1

1

我不相信有一种方法可以将 CAEmitterLayer 与 GPUImageUIElement 一起使用。后者依赖于-renderInContext:对传入的 UI 元素进行光栅化处理,某些 UI 元素如 CAEmitterLayer不会通过此方法渲染

不幸的是,没有其他方法可以向 OpenGL ES 提供这种内容,因此您需要找到另一种生成粒子效果的方法,而不是 CAEmitterLayer。

于 2013-11-04T21:39:12.890 回答