[英]CIFilter not working properly, returns null image
嘿所有我一直在使用一些過濾器。 並非所有這些似乎都能正常工作,例如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,但它只是沒有用於cigloom。 cicolorposterize,ciedges和其他一些。 任何人都知道為什么? 或者如何繞過這個NUll IMAGE?
在iOS上,目前還不支持CIGloom以及其他您遇到過問題的人。 您可以使用此數組結果檢查可用的過濾器:
NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
是的,似乎有些過濾器尚不適用於iOS。 閱讀文檔時有同樣有趣的體驗。 但是,您可以使用代碼檢查以查看適用於iOS的過濾器,如上面的揚聲器。 有些過濾器例如CIVingette,我在文檔中沒有找到,我這樣做也為每個可用於iOS的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); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.