drawRect not being called in my subclass of UIImageView

前端 未结 4 1309
谎友^
谎友^ 2020-12-01 05:24

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

相关标签:
4条回答
  • 2020-12-01 05:47

    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.

    0 讨论(0)
  • 2020-12-01 05:56

    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

    0 讨论(0)
  • 2020-12-01 05:58

    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: in 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.

    0 讨论(0)
  • 2020-12-01 06:01

    Depending on your specific case, it could make sense to use a UIButton with a background image instead of a UIImageView. You could even set userInteractionEnabled to NO and the result would be indistinguishable from a UIImageView.

    In this case your UIButton subclass would simply include:

    - (void)drawRect:(CGRect)rect
    {
      if (kSomethingSpecial) {
        [self setBackgroundImage:[UIImage imageNamed:@"RedBackground.png"] 
                        forState:UIControlStateNormal];
      }
    }
    

    Disclaimer - I would only recommend this if you have plans to use the image as a button at least some of the time. I can't wholeheartedly endorse using a UIButton as a workaround for this drawRect behaviour in UIImageView, as I'm sure Apple has its reasons for writing their API this way, like Mark referenced.

    0 讨论(0)
提交回复
热议问题