“未识别的选择器”当尝试访问CIFilter的outputImage时 [英] "unrecognized selector" when attempting to access CIFilter's outputImage

查看:384
本文介绍了“未识别的选择器”当尝试访问CIFilter的outputImage时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试Core Image(在OS X,10.7.3上),我跑到一堵砖墙上。我确定这是一个愚蠢的我正在做,只是需要一个更熟悉的框架来指出我。

I'm experimenting with Core Image (on OS X, 10.7.3) for the first time and am running into a brick wall. I'm certain this is something silly I'm doing and just need someone more familiar with the framework to point it out to me.

考虑下面的代码(让我们规定 imageURL 是指向磁盘上的JPG的有效文件URL):

Consider the following code (let's stipulate that imageURL is a valid file URL pointing to a JPG on disk):

CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
CIFilter *filter = [CIFilter filterWithName:@"CIAreaAverage" keysAndValues:
                                                  kCIInputImageKey, inputImage,
                                                  kCIInputExtentKey, [inputImage valueForKey:@"extent"],
                                                  nil];
CIImage *outputImage = (CIImage *)[filter valueForKey:@"outputImage"];

运行此代码时,最后一行会触发:

When running this code, the last line triggers:

0   CoreFoundation                      0x00007fff96c2efc6 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff9153cd5e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff96cbb2ae -[NSObject doesNotRecognizeSelector:] + 190
3   CoreFoundation                      0x00007fff96c1be73 ___forwarding___ + 371
4   CoreFoundation                      0x00007fff96c1bc88 _CF_forwarding_prep_0 + 232
5   CoreImage                           0x00007fff8f03c38d -[CIAreaAverage outputImage] + 52
6   Foundation                          0x00007fff991d8384 _NSGetUsingKeyValueGetter + 62
7   Foundation                          0x00007fff991d8339 -[NSObject(NSKeyValueCoding) valueForKey:] + 392



现在,Core Image Filter Reference清楚地说明了CIAreaAverage 返回包含感兴趣区域的平均颜色的单像素图​​像。事实上,当我检查调试器中的过滤器属性(在尝试 valueForKey:调用之前)时,更令人困惑:

Now, the Core Image Filter Reference clearly states that CIAreaAverage "Returns a single-pixel image that contains the average color for the region of interest." Indeed, even more baffling, when I examine the filter attributes in the debugger (before trying the valueForKey: call):

(lldb) po [filter attributes]
(id) $3 = 0x00007fb3e3ef0e00 {
    CIAttributeDescription = "Calculates the average color for the specified area in an image, returning the result in a pixel.";
    CIAttributeFilterCategories =     (
        CICategoryReduction,
        CICategoryVideo,
        CICategoryStillImage,
        CICategoryBuiltIn
    );
    CIAttributeFilterDisplayName = "Area Average";
    CIAttributeFilterName = CIAreaAverage;
    CIAttributeReferenceDocumentation = "http://developer.apple.com/cgi-bin/apple_ref.cgi?apple_ref=//apple_ref/doc/filter/ci/CIAreaAverage";
    inputExtent =     {
        CIAttributeClass = CIVector;
        CIAttributeDefault = "[0 0 640 80]";
        CIAttributeDescription = "A rectangle that specifies the subregion of the image that you want to process.";
        CIAttributeDisplayName = Extent;
        CIAttributeType = CIAttributeTypeRectangle;
        CIUIParameterSet = CIUISetBasic;
    };
    inputImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to process.";
        CIAttributeDisplayName = Image;
        CIUIParameterSet = CIUISetBasic;
    };
    outputImage =     {
        CIAttributeClass = CIImage;
    };
}

outputImage 有 - 作为类型CIImage!

There's outputImage right there - given as type CIImage!

那么,我做错了什么?我看到的所有文档和教程指出 -valueForKey:是访问属性的正确方法,包括 outputImage

So, what am I doing wrong? All the docs and tutorials I have seen indicate that -valueForKey: is the correct way to access attributes, including outputImage.

推荐答案

我相信你的范围是罪魁祸首(不过很奇怪)。当我将区域更改为CIVector *时,它工作。

I believe your extents are the culprit (however strange it is). When I change the extents to be a CIVector* it works.

NSURL *imageURL = [NSURL fileURLWithPath:@"/Users/david/Desktop/video.png"];
CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
CIFilter *filter = [CIFilter filterWithName:@"CIAreaAverage"];
[filter setValue:inputImage forKey:kCIInputImageKey];
CGRect inputExtent = [inputImage extent];
CIVector *extent = [CIVector vectorWithX:inputExtent.origin.x
                                       Y:inputExtent.origin.y
                                       Z:inputExtent.size.width
                                       W:inputExtent.size.height];
[filter setValue:extent forKey:kCIInputExtentKey];
CIImage *outputImage = [filter valueForKey:@"outputImage"];

[inputImage extent]返回一个CGRect,但显然CIVector *效果更好。

[inputImage extent] returns an CGRect, but apparently a CIVector* works better.

这篇关于“未识别的选择器”当尝试访问CIFilter的outputImage时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆