views:

480

answers:

5

I have subclassed UIImageView and tried to override drawRect so I could draw on top of the image using Quartz 2D. I know this is a dumb newbie question, but I'm not seeing what I did wrong. Here's the interface:

#import <UIKit/UIKit.h>

@interface UIImageViewCustom : UIImageView {

}
- (void)drawRect:(CGRect)rect;
@end

And the implementation:

#import "UIImageViewCustom.h"

@implementation UIImageViewCustom

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // do stuff
}

- (void)dealloc {
    [super dealloc];
}
@end

I set a breakpoint on drawRect and it never hits, leading me to think it never gets called at all. Isn't it supposed to be called when the view first loads? Have I incorrectly overridden it?

A: 

Did you actually instantiate a UIImageViewCustom object in your XIB?

Azeem.Butt
Yep. [10 more to go]
Ben Collins
+2  A: 

It'll only get called if the view is visible, and dirty. Maybe the problem is in the code that creates the view, or in your Nib, if that's how you're creating it?

You'll also sometimes see breakpoints failing to get set properly if you're trying to debug a "Release" build.


I somehow missed the first time that you're subclassing UIImageView. From the docs:

Special Considerations

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

So there you have it. Given how easy it is to draw an image into your view using [UIImage drawInRect:], or by using CALayer, there's probably no good reason to subclass UIImageView anyway.

Mark Bessey
shouldn't drawRect get called when the view is first rendered, though?
Ben Collins
Actually, now that you mention it, I remember reading that. Don't know why I couldn't find it again when I was looking for it. Thanks.
Ben Collins
A: 

Not directly answering your question, but may solve your problem:

Why do this with a subclass of UIImageView? Subclassing can always be problematic, especially for classes that aren't designed to be subclassed--and I bet UIImageView isn't. If you just want to draw stuff on top of an image, then create a view with a transparent background, draw whatever you want, and place it directly over the image.

jasoncrawford
You would lose that bet, and many others I suspect.
Azeem.Butt
OK, fine, I looked it up in the documentation and apparently subclassing it is fine. I still suspect this isn't the best way to accomplish the goal.
jasoncrawford
+1  A: 

Try to add

Edit:

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setClearsContextBeforeDrawing:YES];//add this line also
    }
    return self;
}


- (void)setNeedsDisplay{
    [self setNeedsDisplayInRect:self.frame];
}

into your code.

hope this helps.

Thanks,

madhup

Madhup
Thanks for the suggestion, but that didn't work either :-(
Ben Collins
update didn't work either.
Ben Collins
A: 

I guess the UIImageView can not use to override drawRect

Robin