MobileCushion
MobileCushion

Reputation: 7095

Slow drawInRect with same image

I have a custom view inheriting from UIView.

In this view, there is an image which is always the same and I only want to draw on top of that.

However, in order for the image to be drawn, I have to call drawInRect every time the draw cycle runs.

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

    [image drawInRect:self.bounds];
    // drawing....
}

Is there a way to show the image and not to call this method every time drawRect is invoked?

Thank you.

Upvotes: 0

Views: 623

Answers (2)

rob mayoff
rob mayoff

Reputation: 385600

Put the image in a UIImageView underneath your custom UIView instead of drawing it in drawRect:. Make your custom view's background color clearColor so the image shows through where drawRect: doesn't draw anything.

Don't try to make the image view a subview of your custom view. A subview always draws on top of its superview's content. You could make your custom view a subview of the image view, or you could make them siblings.

Upvotes: 2

Kris Gellci
Kris Gellci

Reputation: 9687

In the .h file of the custom class do this:

- (void)  setup:

Then in the .m do this:

-(void) setup {
    //drawing stuff
}

Then in the view did load of whatever the view controller that adds it as a subview is

[myCustomView setup];

Upvotes: -1

Related Questions