Xcode:使用核心图像合成alpha

我想要创build一个CoreImage过滤链,并能够通过合成其单独的效果与alpha或不透明度设置来控制链中每个filter的“强度”,但我没有看到一种方法来与alpha或文档中的不透明度。

我可以跳出核心图像filter链,并与核心graphics上下文我想。

CIColorMatrixfilter可用于更改CIImage的alpha分量,然后您可以将其复合到背景图像上:

CIImage *overlayImage = … // from file, CGImage etc CIImage *backgroundImage = … // likewise CGFloat alpha = 0.5; CGFloat rgba[4] = {0.0, 0.0, 0.0, alpha}; CIFilter *colorMatrix = [CIFilter filterWithName:@"CIColorMatrix"]; [colorMatrix setDefaults]; [colorMatrix setValue:overlayImage forKey: kCIInputImageKey]; [colorMatrix setValue:[CIVector vectorWithValues:rgba count:4] forKey:@"inputAVector"]; CIFilter *composite = [CIFilter filterWithName:@"CISourceOverCompositing"]; [composite setDefaults]; [composite setValue:colorMatrix.outputImage forKey: kCIInputImageKey]; [composite setValue:backgroundImage forKey: kCIInputBackgroundImageKey]; UIImage *blendedImage = [UIImage imageWithCIImage:composite.outputImage]; 

结束了这样做。 代码从这个答案: https : //stackoverflow.com/a/3188761/1408546

 UIImage *bottomImage = inputImage; UIImage *image = filterOutput; CGSize newSize = CGSizeMake(inputImage.size.width, inputImage.size.height); UIGraphicsBeginImageContext( newSize ); [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:_opacity]; UIImage *blendedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();