核心图像的CIColorMatrix过滤器不适用于图像

时间:2013-09-21 09:56:34

标签: iphone ios cifilter ciimage

我想在Image上应用CIColorMatrix:

下面是代码,“过滤器”变量下的所有内容都很好,但它不适用于图像:

- (IBAction)filterImage:(id)sender {

    CIImage *rawImageData;
    rawImageData =[[CIImage alloc] initWithImage:self.imageView.image];


    CIFilter *filter = [CIFilter filterWithName:@"CIColorMatrix"];
    [filter setDefaults];

    [filter setValue:rawImageData forKey:kCIInputImageKey];

    [filter setValue:[CIVector vectorWithX:1 Y:0 Z:0 W:0]
              forKey:@"inputRVector"];
    [filter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0]
              forKey:@"inputGVector"];

    [filter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0]
              forKey:@"inputBVector"];
    [filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1]
              forKey:@"inputAVector"];
    [filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0]
              forKey:@"inputBiasVector"];

    CIImage *filteredImageData  = [filter outputImage];

    UIImage *filteredImage = [[UIImage alloc]init];
    filteredImage = [UIImage imageWithCIImage:filteredImageData];
    self.imageView.image = filteredImage;    
}

1 个答案:

答案 0 :(得分:0)

最后你需要创建一个CGImageRef并释放图像.. 试试这个:

- (IBAction)filterImage:(id)sender {

   CIImage *rawImageData;
   rawImageData =[[CIImage alloc] initWithImage:self.imageView.image];


   CIFilter *filter = [CIFilter filterWithName:@"CIColorMatrix"];
   [filter setDefaults];

   [filter setValue:rawImageData forKey:kCIInputImageKey];

   [filter setValue:[CIVector vectorWithX:1 Y:0 Z:0 W:0]
             forKey:@"inputRVector"];
   [filter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0]
             forKey:@"inputGVector"];

   [filter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0]
             forKey:@"inputBVector"];
   [filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1]
             forKey:@"inputAVector"];
   [filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0]
             forKey:@"inputBiasVector"];

   CIImage *filteredImageData  = [filter outputImage];
   CGImageRef cgimg = [context createCGImage:filteredImageData fromRect:   [filteredImageData extent]];
   newUIImage = [UIImage imageWithCGImage:cgimg];
   CGImageRelease(cgimg);
   [imageView setImage:newUIImage]; 
}

此外,您应该检查构建目标中是否支持您的过滤器。你可以这样测试:

 NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
 NSLog(@"%@", supportedFilters);