我想将动画放到图像中并生成视频,所以我试图将背景图像(CIImage)和动画帧之一(CIImage)与CISourceOverCompositing过滤器合并,但我发现所有透明图像(动画帧)不再变得透明,所有的α值都被舍入(< 0.5-> 0.0,> = 0.5-> 1.0)。也许我选择了错误的过滤器,但我找不到合适的过滤器来满足我的要求。有什么好的建议吗?无论如何,谢谢你的帮助。
我写了一个非常简单的示例代码:
- (void)generateSampleImage {
CIImage *background = [CIImage imageWithContentsOfURL:
[[NSBundle mainBundle] URLForResource:@"background" withExtension:@"png"]];
CIImage *foreground = [CIImage imageWithContentsOfURL:
[[NSBundle mainBundle] URLForResource:@"foreground" withExtension:@"png"]];
CIFilter *filter = [CIFilter filterWithName:@"CISourceOverCompositing"];
[filter setValue:background forKey:kCIInputBackgroundImageKey];
[filter setValue:foreground forKey:kCIInputImageKey];
CIImage *outputImage = [filter outputImage];
self.imageView.image = [self imageWithCIImage:outputImage];
NSURL *baseUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] objectAtIndex:0];
NSURL *outputUrl = [baseUrl URLByAppendingPathComponent:@"output_image.png"];
[UIImagePNGRepresentation(self.imageView.image) writeToFile:outputUrl.path atomically:YES];
}
要与之结合的背景图片: 前景图像与: 使用CISourceOverCompositing过滤器的输出图像: 我想要的理想形象: