继承UIIMage问题

发布于 2024-10-04 00:21:20 字数 1574 浏览 1 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔岸观火 2024-10-11 00:21:20

不建议子类化 UIImage,它是 CGImageRef 之上的 OO 层,并且涉及相当多的魔法

您可以创建一个包装类,并执行 has-a 关系而不是 is-a 关系。对于你的情况来说,这似乎是一个更好的方法。

  • 不是:通道是图像。
  • 相反:频道有一个图像。

像这样:

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

您可能应该更改名称 description 因为它会与 -[NSObject description] 冲突。

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].

硬不硬你别怂 2024-10-11 00:21:20

你发送+imageNamed:,但是你在你的子类中实现了这个方法吗?如果没有,消息将转发到 UIImage,它将返回 UIImage 的实例,而不是 ChImage。由于返回的 UIImage 不响应 ChannelName,因此您的应用程序崩溃。

示例(简化)实现:

+ (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;
}

或者,您可以考虑实现 -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 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.

时光倒影 2024-10-11 00:21:20

不要尝试子类化并向其添加属性。相反,使用 NSObject 创建您自己的类并尝试解决您的问题。因为有一个称为类簇的概念,您必须重写一些基本方法才能使您的类正常工作。

检查我放置的代码:

  @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

而不是使用您的代码:

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

像这样使用它:

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

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...";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文