How to draw an UIImage or directly in -drawRect:?

HelloMoon picture HelloMoon · Nov 2, 2009 · Viewed 47.5k times · Source

I have an UIImage which I want to draw on a UIView. But instead of creating an UIImageView and adding this as a subview, I want to overwrite -drawRect: and draw my UIView directly.

For example, my code looks like:

- (void)drawRect:(CGRect)rect {
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    UIImage *img = [UIImage imageNamed:@"foo.png"];

    // how to draw the directly into the view now?
}

Answer

Ole Begemann picture Ole Begemann · Nov 2, 2009

Call [img drawInRect:rect];.