Re: UIView underlying rects for drawRect
Re: UIView underlying rects for drawRect
- Subject: Re: UIView underlying rects for drawRect
- From: Mike Abdullah <email@hidden>
- Date: Sun, 06 Dec 2015 10:39:33 +0000
> On 6 Dec 2015, at 01:50, Roland King <email@hidden> wrote:
>
> You thought CGContext had it, I thought CALayer had it, neither of them appear to have it!
>
> AFAICT UIView is clearing the entire rectangle before drawing, so that idea doesn’t work either. That was a good way to confirm I was on the wrong road so thanks.
Aha, that is very interesting to know.
>
> So I implemented the code to capture setNeedsDisplayInRect(), drawRect() etc and ensure only one contiguous rectangle goes to the setNeedsDisplayInRect at a time, if there’s another one it waits until drawRect() finishes and then queues that one up. The results were good until they weren’t and I’m really at a loss to explain some of the oddities I saw. It was never going to be stable enough for real use so that idea got tossed.
>
> Current workaround it not to take things off the back, it’s history, I don’t really care, and the code moves the view along anyway so it quickly gets clipped and every few minutes the view is moved to a new chunk of data (it’s like a horizontal scroll view where only 1.5 screens wide of data is shown at a time which are incrementally drawn and then moved to a new chunk every few minutes with one single redraw).
>
> I think a better plan for this whole thing is to use a CALayer with a persistent bitmap for the graph I’m drawing.
>
> Currently I’m seeing a new point come in, working out what that is in view coordinates, calling setNeedsDisplayinRect() on that, then in drawRect I’m converting back to time coordinates, padding out each side so I definitely redraw the whole area cleared including the fuzzy edges of lines a point or two over on the display, then drawing. This is probably nowhere near as efficient as possible.
>
> If I use a CALayer with a bitmap I can put a point on the bitmap asynchronously as soon as it comes in, one point, or one tiny line segment from the previous point, I can do my own clearing when they expire and then all I have to do is arrange the bitmap to be blitted onto the CALayer’s content on a regular basis for update and sort out some full redraw stuff on bounds change stuff.
>
> What’s the right way to synchronise the update of the content property of a CALayer? I could do it every time I add a point, but that sounds a bit unnecessary. Is this where I use CADisplayLink? Is there a recommended format for the bitmap which eases the work the CPU/GPU has to do to transfer it to and from the layer? 8 bit with alpha is all I really need for the plot but if that’s going to trigger a huge conversion every time it’s run through CGBitmapContextCreateImage() and stuffed on the layer I’ll use something else.
CALayer has a mechanism built in for the sort of thing you want to do. Read up on -[CALayer display]. You should be able to override that, or implement the corresponding delegate method in your UIView and perform management of your custom bitmap there. I myself do this in one app to share one bitmap between multiple layers for example.
The other thing worth investigating perhaps is whether CATiledLayer would better suit your drawing needs, or if you could split your custom view up into a series of sub-views, so you only need invalidate slices of one or two of them.
Mike.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden