使用GPUImage 的 GPUImageChromaKeyBlendFilter 将绿色背景替换为透明背景。
代码
// 准备一张透明的图片
UIImage *transparentImg= [UIImage imageNamed:@"MTransparent"];
// 待处理的图片
UIImage *inputImage = [UIImage imageNamed:@"input"];
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
// 创建Filter
GPUImageChromaKeyBlendFilter *filter = [[GPUImageChromaKeyBlendFilter alloc] init];
[filter setColorToReplaceRed:0.0 green:1.0 blue:0.0];
[pic addTarget:filter];
[pic processImage];
// 原始图片
UIImageView *inputImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 200, 100, 100)];
inputImageView.image = inputImage;
[self.view addSubview:inputImageView];
// 创建UIImageView 用于呈现结果
UIImageView *resultImageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
resultImageView.image = [filter imageByFilteringImage:transparentImg];
[self.view addSubview:resultImageView];