无法对图像应用滤镜

时间:2014-06-30 11:45:11

标签: cocoa osx-mavericks core-image quartz-core

我需要将带有默认值的CIZoomBlur应用于图像。我是用以下代码完成的:

NSURL   * url   = [NSURL fileURLWithPath: @"galaxy.png"];
CIImage * image = [CIImage imageWithContentsOfURL: url];
CIContext *context = [[CIContext alloc] init];
CIFilter *filter = [CIFilter filterWithName:@"CIZoomBlur"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
NSImage *image2 = [[NSImage alloc] initWithCGImage:cgImage size:NSSizeFromCGSize(CGSizeMake(640, 1136))];
self.imageView.image = image2;

在日志中我可以看到:

/SourceCache/CoreImage/CoreImage-9.2.8/API/CIFilter.mm:594: CIZoomBlur apply:: Caught exception: CIZoomBlur: id: nil value for argument #0 (src)

图片位于项目内部。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

正确的代码是:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"galaxy" ofType:@"png"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
CIImage * image = [CIImage imageWithData:data];
CIContext *context = [[CIContext alloc] init];
CIFilter *filter = [CIFilter filterWithName:@"CIZoomBlur"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
NSImage *image2 = [[NSImage alloc] initWithCGImage:cgImage size:NSSizeFromCGSize(CGSizeMake(640, 1136))];
self.imageView.image = image2;

如果文件位于应用程序包中,则使用NSURL fileURLWithPath将无法找到它。图像必须为零。感谢trojanfoe