开发者

Inherit from UIIMage problem

I created class ChImage from UIImage

@interface ChImage : UIImage {
@public
    int ChannelId;
    NSString *ChannelName;
    NSString *Description;

}

@property(nonatomic, retain) NSString *ChannelName;
@property(nonatomic, retain) NSString *Description;

@end

@implementation ChImage


@synthesize ChannelName;
@synthesize Description;

@end

And use it in the code:

ChImage *cho = [ChImage imageNamed:img];
        cho = [ChImage imageNamed:img];
        cho.ChannelName = @"something here...";

on the line cho.ChannelName application breaks. I need to extend UIImage because I need some extra property in that class but I don't know what am I doing wrong?

Also when I try to get image from UIImageView I got error. I don't know if this have something with upper problem:

UIImageView *iw = (UIImageView*) sender;
    ChImage *im = (ChImage*) iw.image;
开发者_如何学运维

I just try to cast in another part of code and it works. The problem must be in click handling method. This is that method:

- (void)imageTapped:(UIGestureRecognizer *)sender
{
    UIImageView *iw = (UIImageView*) sender;
    ChImage *im = (ChImage*) iw.image;
    NSLog(im.ChannelName);

}

Becouse this is a uiscrollview with images inside this is how i attached click event to every image:

for (imageToAdd in arrayOfImages)
    {               
        UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];     

        temp.frame = CGRectMake(x, y, 29, 29);
        temp.userInteractionEnabled = YES;      
        x += 29;        

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [temp addGestureRecognizer:tap];    

        [scrollView addSubview:temp];

    }


It is not recommend to subclass UIImage, it is a OO layer on top of CGImageRef and have quite allot of magic involved.

You could create a wrapper class, and do a has-a relationship instead of a is-a relationship. Which seems like a much better approach in your case.

  • Not: Channel is-a Image.
  • Instead: Channel has-a Image.

Like this:

@interface Channel : NSObject {
@private
   NSString* _name;
   NSString* _description;
   UIImage* _image; 
}
// The rest…

You should probably change the name description since it will conflict with -[NSObject description].


You send +imageNamed:, but have you implemented this method in your subclass? If not, the message is forwarded to UIImage which will return an instance of UIImage, not ChImage. Since the returned UIImage does not respond to ChannelName, your app crashes.

Example (simplified) implementation:

+ (UIImage *) imageNamed: (NSString *) imageName
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource: [imageName stringByDeletingPathExtension] ofType: [imageName pathExtension]];
    if ( filePath ) {
        ChImage *theImage = [[ChImage alloc] initWithContentsOfFile: filePath];
        // configure theImage here if necessary
        return [theImage autorelease];
    }
    return nil;
}

Alternatively, you might consider implementing an -initWith… initializer and use it to create instances of your subclass.


Dont try to subclass and add the property to that. Instead create your own class with NSObject and try to solve your problem. Because there is a concept called class cluster where in you will have to override some basic methods to get your class to work propoerly.

Checkout the code I have placed:

  @interface ChImage : NSObject {
    @public
     UIImage *yourImage;
        int ChannelId;
        NSString *ChannelName;
        NSString *Description;

    }

    @property(nonatomic, retain) NSString *ChannelName;
    @property(nonatomic, retain) NSString *Description;
    @property(nonatomic, retain) UIImage *yourImage;

    @end




@implementation ChImage

@synthesize ChannelName;
@synthesize Description;
@synthesize yourImage;

@end

And instead of using your code:

ChImage *cho = [ChImage imageNamed:img];
        cho = [ChImage imageNamed:img];
        cho.ChannelName = @"something here...";

USe it like:

ChImage *cho.yourImage = [UIImage imageNamed:img];
        cho = [UIImage imageNamed:img];
        cho.ChannelName = @"something here...";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜