CIFilter не работает должным образом, возвращает нулевое изображение - PullRequest
3 голосов
/ 28 декабря 2011

Привет всем, я использовал некоторые фильтры. Не все из них работают нормально, как, например, CISepiaTone и CIHueAdjust. Недавно я попробовал фильтр CIGLoom, и он возвращает нулевое изображение.

-(UIImage*)getGloom:(UIImage*)anImage{
    CGImageRef cgimage = anImage.CGImage;
    CIImage *cimage = [CIImage imageWithCGImage:cgimage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGloom"];
    [filter setDefaults];
    [filter setValue: cimage forKey: @"inputImage"];
    [filter setValue: [NSNumber numberWithFloat: 25]
             forKey: @"inputRadius"];
    [filter setValue: [NSNumber numberWithFloat: 0.75]
             forKey: @"inputIntensity"];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *ciimage = [filter outputImage];
    CGImageRef cgimg = [context createCGImage:ciimage fromRect:[ciimage extent]];
    UIImage *uimage = [UIImage imageWithCGImage:cgimg scale:1.0f orientation:UIImageOrientationUp];

    CGImageRelease(cgimg);

    return uimage; }

Я на самом деле получил этот код из мирового тура techtalk в этом году, и он работает для CISepiaTone, но он не работает для сигарет. cicolorposterize, cedges и некоторые другие. Кто-нибудь есть идеи, почему? или как обойти это NUll IMAGE?

Ответы [ 2 ]

5 голосов
/ 06 января 2012

CIGloom и другие, с которыми у вас возникли проблемы, пока не поддерживаются в iOS. Вы можете проверить, какие фильтры у вас есть, используя этот результат массива:

NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
4 голосов
/ 19 апреля 2012

Да, похоже, некоторые фильтры пока недоступны для iOS.Был такой же забавный опыт при чтении документации.Однако вы можете проверить с помощью кода, чтобы увидеть, какой фильтр доступен для iOS, как указано выше.Некоторый фильтр, например CIVingette, который я не нашел в документации, я сделал это, чтобы также получить значения-параметры для каждого доступного фильтра для iOS на 5.1.

NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
for (CIFilter *filter in supportedFilters) {
    NSString *string = [NSString stringWithFormat:@"%@",[[CIFilter filterWithName:(NSString *)filter] inputKeys]];
    NSLog(@"%@ %@", filter, string);
}

Ответ:

...
2012-04-19 14:02:55.597 ImageFiltering[12190:707] CIVibrance (
    inputImage,
    inputAmount
)
2012-04-19 14:02:55.599 ImageFiltering[12190:707] CIVignette (
    inputImage,
    inputIntensity,
    inputRadius
)
2012-04-19 14:02:55.601 ImageFiltering[12190:707] CIWhitePointAdjust (
    inputImage,
    inputColor
)
...

Обратите внимание, что в будущем (или кто-то уже знает, где вы можете прочитать об этом больше) вы, вероятно, захотите прочитать документацию.Это просто я играю в детектива, чтобы обойти это, потому что я не нашел никакой документации по некоторым фильтрам, как я упоминал ранее.

Вот пример, который я получил с CIVingette из предыдущей информации:

- (void)displayVingetteFilterWithImage{
    // Get image and set it as CIImage
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    CIImage *image = [CIImage imageWithContentsOfURL:fileNameAndPath];

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

    // Set filter to image, in this case CIVignette, knowing it uses inputImage, inputIntensity and inputRadius from previous log-response.
    CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"];
    [vignette setDefaults];
    [vignette setValue: image forKey: @"inputImage"];
    [vignette setValue: [NSNumber numberWithFloat: 5.0] forKey: @"inputIntensity"];
    [vignette setValue: [NSNumber numberWithFloat: 30.0] forKey: @"inputRadius"];

    // Attach the CIImage to CGImageRef and attach it as UIImage
    CIImage *result = [vignette valueForKey: @"outputImage"];
    CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
    UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];

    // Attach UIImage to UIImageView in self.view, also position it, just for fun.
    UIImageView *imageView = [[UIImageView alloc] initWithImage:targetImage];
    [self.view addSubview:imageView];
    [imageView setImage:targetImage];
    imageView.frame = CGRectMake(0.0, 10.0, imageView.frame.size.width, imageView.frame.size.height);

    //  Release CGImageRef we created earlier.
    CGImageRelease(cgImageRef);
}
...