views:

30

answers:

1

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.

+2  A: 

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:

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

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).

Jeff Laing
It doesn't matter what Apple names their method if the questioner comes up with the same selector, and vice versa—and, in fact, there could very well be a hidden `-[CIImage initWithImage:]` (or `initWithNSImage:`) method, which is why it's a good idea to mark any methods you add in categories with a prefix or suffix unique to your application.
Peter Hosey
I do include my category's header file and still got the compiler warning. Eventually I gave up and name my method "initWithNSImage" instead.
adib