继承UIIMage问题
我从 UIImage 创建了类 ChImage
@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
并在代码中使用它:
ChImage *cho = [ChImage imageNamed:img];
cho = [ChImage imageNamed:img];
cho.ChannelName = @"something here...";
在 cho.ChannelName 应用程序中断行上。我需要扩展 UIImage 因为我需要该类中的一些额外属性,但我不知道我做错了什么?
另外,当我尝试从 UIImageView 获取图像时,出现错误。我不知道这是否有上面的问题:
UIImageView *iw = (UIImageView*) sender;
ChImage *im = (ChImage*) iw.image;
我只是尝试转换代码的另一部分并且它有效。问题一定出在点击处理方法上。这就是那个方法:
- (void)imageTapped:(UIGestureRecognizer *)sender
{
UIImageView *iw = (UIImageView*) sender;
ChImage *im = (ChImage*) iw.image;
NSLog(im.ChannelName);
}
因为这是一个 uiscrollview,里面有图像,这就是我将单击事件附加到每个图像的方式:
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不建议子类化
UIImage
,它是CGImageRef
之上的 OO 层,并且涉及相当多的魔法。您可以创建一个包装类,并执行 has-a 关系而不是 is-a 关系。对于你的情况来说,这似乎是一个更好的方法。
像这样:
您可能应该更改名称
description
因为它会与-[NSObject description]
冲突。It is not recommend to subclass
UIImage
, it is a OO layer on top ofCGImageRef
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.
Like this:
You should probably change the name
description
since it will conflict with-[NSObject description]
.你发送
+imageNamed:
,但是你在你的子类中实现了这个方法吗?如果没有,消息将转发到 UIImage,它将返回 UIImage 的实例,而不是 ChImage。由于返回的 UIImage 不响应ChannelName
,因此您的应用程序崩溃。示例(简化)实现:
或者,您可以考虑实现
-initWith...
初始值设定项并使用它来创建子类的实例。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 toChannelName
, your app crashes.Example (simplified) implementation:
Alternatively, you might consider implementing an
-initWith…
initializer and use it to create instances of your subclass.不要尝试子类化并向其添加属性。相反,使用 NSObject 创建您自己的类并尝试解决您的问题。因为有一个称为类簇的概念,您必须重写一些基本方法才能使您的类正常工作。
检查我放置的代码:
而不是使用您的代码:
像这样使用它:
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:
And instead of using your code:
USe it like: