Xcode:使用核心图像与alpha合成

Mrw*_*lfy 6 image core-graphics core-image ios

我想创建一个CoreImage过滤器链,并且能够通过将其单独的效果与alpha或不透明度设置合成来控制链中每个过滤器的"强度",但我没有看到与alpha合成的方法或文档中的不透明度.

我猜想我可以跳出核心图像过滤器链和复合核心图形上下文.

mrw*_*ker 20

所述CIColorMatrix过滤器可用于改变CIImage,的α分量可以然后复合到背景图像:

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];
Run Code Online (Sandbox Code Playgroud)


Mrw*_*lfy -4

最后就这样做了。此答案中的代码: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();
Run Code Online (Sandbox Code Playgroud)