我正在尝试覆盖UIView
类ParticleEmitter
中实现的粒子发射器。
当我尝试将UIView
添加为uiElementInput
时,它不会显示。相机输入仍然有效,但不显示粒子发射器。
更新代码:
//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中使用Text的示例代码:
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];
// ============================================= ============
有什么建议吗?
答案 0 :(得分:1)
我不相信有一种方法可以将CAEmitterLayer与GPUImageUIElement一起使用。后者依赖于-renderInContext:
来栅格化您传递给它的UI元素,以及一些UI元素,如CAEmitterLayer will not be rendered via this method。
不幸的是,没有其他方法可以为OpenGL ES提供此类内容,因此您需要找到另一种生成粒子效果的方法而不是CAEmitterLayer。