1

I'm trying to subclass UIImageView to add some custom functionality in it. I started trying to do the basics, just init an object with a given image and display it on the screen.
Using UIImageView everything works fine, but if i change my object from UIImageView to my custom class, no image is displayed.
Here's what i've done so far:

//Imager.h
#import <UIKit/UIKit.h>
@interface Imager : UIImageView {

}

-(id) init;
-(void) retrieveImageFromUrl: (NSString *)the_URL;

@end

//Imager.m
#import "Imager.h"
@implementation Imager
@synthesize image;


-(id)init
{
    self = [super init];
    return self;
}

-(void) retrieveImageFromUrl: (NSString *)the_URL{

     //Not implemented yet
}
@end

So, i am using this statment: cell.postImage.image = [UIImage imageNamed:@"img.png"];
Before this is executed, i also have postImage = [[UIImageView alloc] init];

If postImage is declared as UIImageView everything works as expected. But if i change it to Imager instead, then nothing is displayed. What am i missing?

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
CdB
  • 4,738
  • 7
  • 46
  • 69
  • Why do you have `@synthesize image;`? You don't have a property defined? It looks like something is missing in your example. – Kobski Apr 04 '11 at 13:29

2 Answers2

4

You're synthesizing image, thus blocking calls to setImage on the superclass (i.e. UIImageView) when you attempt to use the dot notation to set the image.

Remove this line:

@synthesize image;
jnic
  • 8,695
  • 3
  • 33
  • 47
  • Thanks for your instant reply! That's what was causing the problem. Just to make it clear, is it a general rule, if i don't want to customize getters/ setters in my subclass, i should not use @synthesize? – CdB Apr 04 '11 at 13:35
  • @CrisDeBlonde `@synthesize` exists to create accessor methods for you, it's up to you whether you use it when customizing getters and setters. See [Declared Properties](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html). – jnic Apr 04 '11 at 13:51
  • Thanks a lot for answering. Now i think i've got it more clear. – CdB Apr 04 '11 at 14:02
3

I would suggest using an Objective-C "Category" instead of subclassing the UIImageView. As long as you don't have to add any member variables, a Category is a better solution. When you use a category you can call your extended functions on any instance of the original class (in your case UIImageView. That removes the need for you to consciously use your subclass anywhere you might want to use your new functions.

You can just do the following in a header:

@interface UIImageView (UIImageView+URL)

-(void) retrieveImageFromUrl: (NSString *)the_URL;

@end

Then in an implimentation file:

@implimentation UIImageView (UIImageView+URL)    

-(void) retrieveImageFromUrl: (NSString *)the_URL
{
    // implimentation
}

@end

Then wherever you want to use your new function on a UIImageView, you just need to include the header file.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • thanks a lot for replying! It's true i didn't know much about categories, but in this case later on, i will add some member variables that are needed, as well as some more methods. This is only the beginning of the subclass construction. Anyway, thanks again for answering! – CdB Apr 04 '11 at 13:39