Reputation: 2207
This is a super-basic question. I want to use a class from Matt Gemmell's MGImageUtilities in my app. I've added the classes UIImage+ProportionalFill.h
and UIImage+ProportionalFill.m
to my project. They define the method imageScaledToFitSize
. But when I try to run the app, I get an unrecognized selector sent to instance
error. Obviously I have to make the method available to the class in which I want it to run, but I don't know how to do that. I have #import UIImage+ProportionalFill.h
in both the .h and .m files of the view controller, and in the .h file I've added
@interface UIImage (ProportionalFill)
- (void)imageScaledToFitSize;
@end
But I'm still getting the error. I assume there's something very fundamental that I'm failing to do, but I don't know what it is.
Your thoughts?
UPDATE: My project has the following classes: UIImage+ProportionalFill
, 'GHViewController, and '
GHAppDelegate.
UIImage+ProportionalFill` contains the following declaration (and corresponding implementation):
- (UIImage *)imageScaledToFitSize:(CGSize)size;
In GHViewController.m
, I have the following code:
UIImage *img = [self createImage];
UIImage *pic;
if (self.serviceType==SLServiceTypeFacebook)
{
CGSize size = CGSizeMake((404*320)/([[UIScreen mainScreen] bounds].size.height - 64), 404);
pic = [img imageScaledToFitSize:size];
}
That's the only place I'm calling imageScaledToFitSize
.
Upvotes: 0
Views: 140
Reputation: 69027
If you have included the .m file, you should not get the unrecognized selector sent to instance
error. So, I suspect that either the .m file was not correctly added to your target, or that you misspelled something when calling the method, or sent it to the wrong object (not an instance of UIImage
).
If you provide more info about the way you are calling the method (i.e., on which object) and the exact console log output, I might be able to help you further.
To check if the .m
file is actually assigned to your target, go to your project info pane in Xcode and show the build phases pane for the target; there you will find a list of all the modules included when building it.
As a general note:
including the .h
file for your category will prevent the compiler from emitting a warning about an undefined method; not including the .h
file will not prevent the method from being actually called, if the implementation is available;
if you do not include the .m
file you get the exception with the unrecognized selector sent to instance
.
Upvotes: 2
Reputation: 25907
The category provides the implementation of the method. What I think it might be happening is that you are adding the category to the wrong class. It's like adding a category to your MyClass
and the category calls something like:
[self count]; // Method from an NSArray
Upvotes: 1