隐藏的 [CIImage initWithImage:] 方法?
Apple 是否声明了我不知道的方法 [CIImage initWithImage:(CIImage*)]
?据我所知,具有该签名的唯一方法是[CISampler initWithImage:]
。但是当我尝试提供自己的方法时,编译器警告我该方法已经存在。
背景:我正在尝试创建一种将 NSImage
实例转换为 CIImage
的便捷方法。然后,我创建了一个接受 NSImage
实例的类别方法 [CIImage initWithImage:]
。
这是类别方法声明:
@interface CIImage (QuartzCoreExtras)
-(id) initWithImage:(NSImage*) img;
@end
我尝试在 NSImageView
子类中使用它来缓存图像的 CoreImage 版本:
-(void) setImage:(NSImage *)newImage {
[super setImage:newImage];
[ciImage release];
ciImage = [[CIImage alloc] initWithImage:newImage];
}
但是当我编译上述方法时,我收到一条警告,说其他人已经定义了该方法,并且它采用不同的参数:
warning: incompatible Objective-C types 'struct NSImage *', expected 'struct CIImage *' when passing argument 1 of 'initWithImage:' from distinct Objective-C type
从 XCode 中的“跳转到定义”选项,该方法的唯一其他实现(除了我自己的实现)是 [CISampler initWithImage:(CIImage*]
我真的对这个问题感到困惑——我做错了什么吗?
为了完整起见,这里是 [CIImage initWithImage:]
的方法主体:
@implementation CIImage (QuartzCoreExtras)
-(id) initWithImage:(NSImage*) img {
NSData* tiffData = [img TIFFRepresentation];
NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData:tiffData];
return [self initWithBitmapImageRep:bitmap];
}
@end
提前致谢。
Is Apple declaring method [CIImage initWithImage:(CIImage*)]
that I'm not aware of? The only method with that signature that I'm aware of is [CISampler initWithImage:]
. But when I tried to provide my own method, the compiler warns me saying that the method already exists.
Background: I'm trying to create a convenience method that converts an NSImage
instance to CIImage
. Then I created a category method [CIImage initWithImage:]
that takes in an NSImage
instance.
Here is the category method declaration:
@interface CIImage (QuartzCoreExtras)
-(id) initWithImage:(NSImage*) img;
@end
I tried to use it in an NSImageView
subclass to cache the CoreImage version of the image:
-(void) setImage:(NSImage *)newImage {
[super setImage:newImage];
[ciImage release];
ciImage = [[CIImage alloc] initWithImage:newImage];
}
But when I compile the above method, I get a warning saying that someone else have already defined the method and it takes a different parameter:
warning: incompatible Objective-C types 'struct NSImage *', expected 'struct CIImage *' when passing argument 1 of 'initWithImage:' from distinct Objective-C type
From the "Jump to Definition" option in XCode, the only other implementation of the method (besides my own implementation) is [CISampler initWithImage:(CIImage*]
. I'm really puzzled by this issue -- is there anything I did wrong?
Just for completeness' sake, here is the method body for [CIImage initWithImage:]
:
@implementation CIImage (QuartzCoreExtras)
-(id) initWithImage:(NSImage*) img {
NSData* tiffData = [img TIFFRepresentation];
NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData:tiffData];
return [self initWithBitmapImageRep:bitmap];
}
@end
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据猜测,您尚未将定义类别的标头包含到 .m 文件中,
这里的技巧是 [CIImage alloc] 返回“id”类型的值。因此,他们不知道将搜索仅限于 CIImage 类,而是查看所有类,这就是他们在 CISampler 中找到定义的原因。
我认为如果您将代码更改为:
您可能会绕过警告,因为编译器将更多地了解要使用哪个版本的 initWithImage: 。
遗憾的是,做你所做的事情是很糟糕的。将您的方法重命名为 initWithNSImage: - 从长远来看,它将更容易支持。
(苹果确实应该将他们的方法命名为 initWithNSImage: 但他们似乎通常保留从其方法中删除“NS”的权利,并且由于这是他们的框架,所以他们获胜)。
At a guess, you haven't included the header that defines your category into your .m file,
The trick here is that [CIImage alloc] returns a value of type 'id'. As such, they don't know to limit the search just to the CIImage class, and instead look through all classes, which is why they find the definition in CISampler.
I think if you change your code to this:
you may get past the warning, since the compiler will have more of a clue about which version of initWithImage: to use.
Having sad that, its bad form to do what you've done. Rename your method to initWithNSImage: - in the long run, it'll be easier to support.
(Apple should really have named their method initWithNSImage: but they seem to have generally reserved the right to remove the "NS" from their methods, and since its their framework, they win).