5

我正在尝试使用混合模式(如叠加或乘法)创建一个 CIFilter。相关代码:

// Let's try a filter here
// Get the data
NSData *imageData = UIImageJPEGRepresentation(image, 0.85);
// Create a CI Image
CIImage *beginImage = [CIImage imageWithData:imageData];
CIImage *overlay = [CIImage imageWithColor:[CIColor colorWithRed:0.7 green:0.75 blue:0.9 alpha:0.75]];
// Create a context
CIContext *context = [CIContext contextWithOptions:nil];
// Create filter
CIFilter *filter = [CIFilter filterWithName:@"CIOverlayBlendMode" 
                                  keysAndValues:@"inputImage", beginImage,
                                                @"inputBackgroundImage", overlay,
                                                nil];

其他过滤器工作正常(如棕褐色调),但使用需要“inputBackgroundImage”键的过滤器,我得到一个空白/空结果......所以我的背景图像似乎有问题。

如何通过在图像上放置纯色来使用混合模式滤镜?

4

2 回答 2

6

在主题启动器的代码中没问题,应该只有一个修复。

NSData *imageData = UIImageJPEGRepresentation(image, 0.85);
// Create a CI Image
CIImage *beginImage = [CIImage imageWithData:imageData];
CIImage *overlay = [CIImage imageWithColor:[CIColor colorWithRed:0.7 green:0.75 blue:0.9 alpha:0.75]];

//You should crop the generated image to beginImage size because generated image's size is infinite. I don't know why, but CIOverlayBlendMode isn't tolerant to infinite size
    overlay = [overlay imageByCroppingToRect:CGRectMake(beginImage.extent.origin.x, beginImage.extent.origin.y, beginImage.extent.size.width, beginImage.extent.size.height)];

// Create a context
CIContext *context = [CIContext contextWithOptions:nil];
// Create filter
CIFilter *filter = [CIFilter filterWithName:@"CIOverlayBlendMode" 
                                      keysAndValues:@"inputImage", beginImage,
                                                    @"inputBackgroundImage", overlay,
                                                    nil];
于 2014-02-14T19:19:36.833 回答
3

这有效:

//assume beginImage is CIImage you want to tint            
CIImage* outputImage = nil;

//create some blue
CIFilter* blueGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor* blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"];
[blueGenerator setValue:blue forKey:@"inputColor"];
CIImage* blueImage = [blueGenerator valueForKey:@"outputImage"];

//apply a multiply filter
CIFilter* filterm = [CIFilter filterWithName:@"CIMultiplyCompositing"];
[filterm setValue:blueImage forKey:@"inputImage"];
[filterm setValue:beginImage forKey:@"inputBackgroundImage"];
outputImage = [filterm valueForKey:@"outputImage"];
于 2012-10-01T16:03:13.410 回答