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.
If you have included the .m file, you should not get the
unrecognized selector sent to instanceerror. 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 ofUIImage).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
.mfile 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
.hfile for your category will prevent the compiler from emitting a warning about an undefined method; not including the.hfile will not prevent the method from being actually called, if the implementation is available;if you do not include the
.mfile you get the exception with theunrecognized selector sent to instance.