2010-07-18 74 views
0

蘋果是否聲明我不知道的方法[CIImage initWithImage:(CIImage*)]?我知道的簽名的唯一方法是[CISampler initWithImage:]。但是當我試圖提供我自己的方法時,編譯器警告我說該方法已經存在。隱藏[CIImage initWithImage:]方法?

背景:我試圖創建一個便利的方法,將NSImage實例轉換爲CIImage。然後,我創建了一個類別方法[CIImage initWithImage:],它採用NSImage實例。

這裏是類方法聲明:

@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 

在此先感謝。

回答

2

在猜測,您還沒有定義您的類別到您的.m文件頭,

這裏的竅門是,[CIImage頁頭]返回類型爲「身份證」的價值。因此,他們不知道將搜索僅限於CIImage類,而是查看所有類,這就是爲什麼他們在CISampler中找到定義的原因。

我想如果你改變你的代碼如下:

ciImage = [ ((CImage*)[CIImage alloc]) initWithImage:newImage]; 

你可能會得到過去的警告,因爲編譯器將有更多的線索哪些initWithImage的版本:使用。

傷心的是,它的糟糕形式去做你所做的事情。將你的方法重命名爲initWithNSImage: - 從長遠來看,它會更容易支持。 (蘋果應該真的命名他們的方法initWithNSImage:但他們似乎一般保留從他們的方法中刪除「NS」的權利,並且因爲它的框架,他們贏了)。

+0

如果提問者使用相同的選擇器,那麼Apple命名他們的方法並不重要,反之亦然 - 實際上,可能會有一個隱藏的' - [CIImage initWithImage:]'(或' initWithNSImage:'方法,這就是爲什麼標記任何您添加到類別中的方法的好方法,它的前綴或後綴對應用程序是唯一的。 – 2010-07-18 07:40:02

+0

我確實包含了我的類別的頭文件,並且仍然收到編譯器警告。最終我放棄了,並命名我的方法「initWithNSImage」。 – adib 2010-07-20 04:03:18