0

我想在 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;    
}
4

1 回答 1

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);
于 2013-10-10T10:27:07.547 回答