CIFilter输出宽度和高度为零

时间:2012-06-24 01:58:10

标签: ios core-image

我正在尝试过滤从imagePicker获得的图像。 原始图像与我的代码完全一致,但是当我尝试使用CIFilter过滤它时,生成的UIImage“filteredImage”的大小为0x0像素。

我在这里发布了完整的功能,它是图像选择器委托方法。

更新:过滤“CISepiaTone”可以正常工作并为我提供正确尺寸的图像。但是CIColorMonochrome也可以在iOS上使用,我通过向CIFilter询问可用的过滤器选项来检查。

UPDATE 2:找到它 - inputColor参数必须是CIColor类型而不是UIColor,我在参考中忽略了它。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Filter the image
    CIImage *filterImage = [[CIImage alloc] initWithCGImage:image.CGImage];
    CIFilter *colorMono = [CIFilter filterWithName:@"CIColorMonochrome"];
    [colorMono setDefaults];
    [colorMono setValue:filterImage forKey:@"inputImage"];
    [colorMono setValue:[UIColor yellowColor] forKey:@"inputColor"];

    CIImage *outputImage = [colorMono outputImage];
    CIContext *con = [CIContext contextWithOptions:nil];
    CGImageRef filteredImageRef = [con createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *filteredImage = [UIImage imageWithCGImage:filteredImageRef scale:1.0f orientation:UIImageOrientationUp];
    // filteredImage dimensions are 0x0 px here!

    CGImageRelease(filteredImageRef);

    // Save image in imageStore
    CFUUIDRef uniqueId = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef uniqueIdStringRef =  CFUUIDCreateString(kCFAllocatorDefault, uniqueId);

    NSString *key = (__bridge NSString *)uniqueIdStringRef;
    [[FPImageStore sharedStore] setImage:filteredImage forKey:key];
    imageKey = key;

    CFRelease(uniqueId);
    CFRelease(uniqueIdStringRef);

    // Dismiss imagePicker viewController
    [self dismissViewControllerAnimated:YES completion:nil];    
}

1 个答案:

答案 0 :(得分:0)

inputColor参数必须是CIColor类型而不是UIColor,我在参考中忽略了它。