k06a
k06a

Reputation: 18765

drawInRect method works too slow

Now i have following:

- (void)drawRect
{
    // some drawing

    [bgImage drawinRect:self.bounds];

    // some drawing
}

I have more than 40 views with text and some marks inside. I need to repaint all these views on user tapping - it should be really fast!

I instrumented my code and saw: 75% of all execution time is [MyView drawRect:] and 95% of my drawRect time is [bgImage drawinRect:self.bounds] call. I need to draw background within GPU nor CPU. How is it possible?

What i have tried:

  1. Using subviews instead of drawRect. In my case it is very slow because of unkickable color blending.
  2. Adding UIImageView as background don't helps, we can't draw on subviews ...
  3. Adding image layer to CALayer? Is it possible?

UPDATE:

Now i am trying to use CALayer instead of drawInRect.

- (void)drawRect
{
    // some drawing

    CALayer * layer = [CALayer layer];
    layer.frame = self.bounds;
    layer.contents = (id)image.CGImage;
    layer.contentsScale = [UIScreen mainScreen].scale;
    layer.contentsCenter = CGRectMake(capInsects.left / image.size.width,
                                      capInsects.top / image.size.height,
                                      (image.size.width - capInsects.left - capInsects.right) / image.size.width,
                                      (image.size.height - capInsects.top - capInsects.bottom) / image.size.height);

    // some drawing
}

Nothing instead of this new layer is visible right now. All my painting is under this layer i think...

Upvotes: 2

Views: 1096

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

I would use UILabels; you can reduce the cost of blending by giving the labels a background color or setting shouldRasterize = YES on the layer of the view holding those labels. (You'll probably also want to set rasterizationScale to be the same as [[UIScreen mainScreen] scale]).

Another suggestion:

There are lots of open-source calendar components that have probably had to try and solve the same issues. Perhaps you could look and see how they solved them.

Upvotes: 1

Related Questions