5

我有一个项目有几个核心图像过滤器,每个都连接到不同的滑块。一切正常,但我还没有想出将结果从一个过滤器传递到下一个过滤器的最佳方法。每次我更改任何其他滑块时,它们都会重置。原因是从导入图像创建的原始图像被每个过滤器绘制为输入图像。但不知道如何解决这个问题。

我正在尝试考虑将多个过滤器的结果传递到一个输出图像的最佳方法。

这是项目:owolf.net/uploads/StackOverflow/CoreImageFilter.zip

以及下面粘贴的一些viewControler的代码:

- (void)viewDidLoad
{
    //Create CIImage
    UIImage *aUIImage = [imageView image];
    CGImageRef aCGImage = aUIImage.CGImage;
    aCIImage = [CIImage imageWithCGImage:aCGImage];

    //Create context
    context = [CIContext contextWithOptions:nil];

    saturationFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues: @"inputImage", aCIImage, nil];
    brightnessFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues: @"inputImage", aCIImage, nil];
    contrastFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues: @"inputImage", aCIImage, nil];
        [super viewDidLoad];
}


- (IBAction)saturationSliderValueChanged:(id)sender {
    outputImage = [saturationFilter outputImage];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    newUIImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    [imageView setImage:newUIImage];   
}

- (IBAction)brightnessSliderValueChanged:(id)sender {
    [brightnessFilter setValue:[NSNumber numberWithFloat:brigtnessSlider.value] forKey: @"inputBrightness"];
    outputImage = [brightnessFilter outputImage];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    newUIImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    [imageView setImage:newUIImage];
}

- (IBAction)contrastSliderValueChanged:(id)sender { 
    [contrastFilter setValue:[NSNumber numberWithFloat:contrastSlider.value] forKey: @"inputContrast"];
    outputImage = [contrastFilter outputImage];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    newUIImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    [imageView setImage:newUIImage];
}
4

2 回答 2

12

您需要选择应用过滤器的顺序,然后将第一个过滤器的输出用作第二个过滤器的输入图像,依此类推。这是我的一个项目中的一个示例:

CIImage *adjustedImage = self.originalImage;

[self.vibranceFilter setValue:adjustedImage forKey:@"inputImage"];
adjustedImage = [self.vibranceFilter outputImage];

[self.hueFilter setValue:adjustedImage forKey:@"inputImage"];
adjustedImage = [self.hueFilter outputImage];

[self.vignetteFilter setValue:adjustedImage forKey:@"inputImage"];
adjustedImage = [self.vignetteFilter outputImage];

CGImageRef cgImage = [self.imageContext createCGImage:adjustedImage fromRect:[adjustedImage extent]];

self.imageView.image = [UIImage imageWithCGImage:cgImage];

CGImageRelease(cgImage);

该值adjustedImage有效地累积了 Core Image 滤镜的效果。这一切都在它自己的方法中进行,每当您的任何滑块值发生变化时都会调用该方法。

于 2012-08-13T04:47:25.077 回答
2

问题是您将所有这些的输入图像设置为aCIImage. 你需要做这样的事情:

[filter1 setValue:aCIImage forKey:@"inputImage"];

filter1Output = [filter1 outputImage];
[filter2 setValue:filter1Output forKey:@"inputImage"];

等等

于 2012-08-13T04:47:58.227 回答