【问题标题】:Cannot apply filter to image无法对图像应用滤镜
【发布时间】:2014-06-30 11:45:10
【问题描述】:

我需要将具有默认值的 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)

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

【问题讨论】:

  • 你真的检查过image是否不是nil吗?
  • 它不是 nil 。我已经检查了
  • 如果文件位于应用程序包中,则使用NSURL fileURLWithPath不会找到它。图片必须是nil

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-11-18
    • 2021-06-27
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多