我正在编写一个函数,将过滤器应用于图像并返回新图像。我写了以下代码:
{{1}}
我遇到的问题是该函数返回的是nil照片,而不是应用过滤器的照片。有趣的是,如果我将过滤器名称更改为@“CIVignetteEffect”,它将起作用。我不明白为什么一个过滤器工作但另一个不能。我找到了以下链接中的两个过滤器:https://developer.apple.com/library/tvos/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIPhotoEffectTransfer
答案 0 :(得分:2)
我认为过滤器的正确名称是CIPhotoEffectTransfer,而不是CIphotoeffectTransfer。
试试这段代码......过去我用它进行过滤,所以我知道它有效:
+ (UIImage*)applyFilter:(UIImage*) photo {
UIImageOrientation orientation = photo.imageOrientation;
CIImage* image = [CIImage imageWithCGImage:photo.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newPhoto = [UIImage imageWithCGImage:cgimg scale:1.0 orientation:orientation];
CGImageRelease(cgimg);
context = nil;
return newPhoto;
}
答案 1 :(得分:0)
试试这个,
我们已实施 CIFilter 效果,
//CIVignette Effect
CIContext *imageContext = [CIContext contextWithOptions:nil];
CIImage *image = [[CIImage alloc] initWithImage:inputimage];
CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"];
[vignette setDefaults];
[vignette setValue: image forKey: @"inputImage"];
[vignette setValue: [NSNumber numberWithFloat: 1.0] forKey: @"inputIntensity"];
[vignette setValue: [NSNumber numberWithFloat: 10.00 ] forKey: @"inputRadius"];
CIImage *result = [vignette valueForKey: @"outputImage"];
CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];
有关多重效果的详细实现,您可以参考此GitHub项目文件ImageFilter
希望这个答案对某些人有所帮助。