When combining two images with CIFilter (CISourceOverCompositing), the lost alpha channel

I want to put an animation in an image and generate it as a video. So I try to combine the background image (CIImage) and one of the animation frames (CIImage) with the CISourceOverCompositing filter, but I found that all transparent images (animation frames) become non-transparent, all alpha values ​​are rounded (<0.5-> 0 , 0,> = 0.5-> 1.0). I may have chosen the wrong filter, but I can’t find a suitable one for my requirement. Are there any good suggestions? Thank you for your help.

I wrote an example code that is very simple:

- (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];
}

: the background image to combine with : the foreground image to combine with CISourceOverCompositing: the output image with CISourceOverCompositing filter , : the ideal image that I want

+4

All Articles