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 ofCGImageRefand 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.
Like this:
You should probably change the name
descriptionsince it will conflict with-[NSObject description].