2011-12-28 19 views
3

嘿,我一直在使用一些過濾器。並不是所有的人都像正常工作一樣,例如CISepiaTone和CIHueAdjust。 最近我試過CIGLoom過濾器,它返回一個空圖像。CIFilter工作不正常,返回null圖片

-(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,但它只是失敗的cigloom。 cicolorposterize,ciedges和其他一些。任何人都知道爲什麼?或如何解決這個NUll IMAGE?

回答

5

CIGloom以及其他您一直遇到的問題在iOS上尚未得到支持。您可以檢查所使用此陣列結果可用它過濾:

NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn]; 
4

是的,這似乎有些過濾器是不支持iOS呢。閱讀文檔時有相同的樂趣。但是,您可以使用代碼來查看iOS上可用的過濾器,如上面的說明。一些過濾器例如CIVingette,我沒有在文檔中找到,我這樣做也爲iOS上的每個可用過濾器獲取值參數。

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); 
}
+0

投票您只需爲最終結束我的痛苦與CIVignette。爲什麼屬性表示最大強度是1,最大半徑是2?!!!我應該只是嘗試了不同的數字。 – Meroon 2012-08-17 09:27:04