Question regarding NSView's drawRect

  • Hi all, I have a question regarding NSView. I have an application that
    keep many subclassed NSView objects. Users shall be able to navigate
    around and depending on their  action, certain NSView objects will be
    shown and other will be hidden or moved to other window

    Now.. I actually only need to use drawRect once for every object -
    when I first created the objects. I will neither need to scroll nor do
    anything that will require redrawing of the NSView objects. By right,
    it should be safe to release all the image data objects I use after
    the drawRect returns; and that is my intention in order to minimize
    the memory usage. But it seems that there are certain occasions where
    the Application would call drawRect on its own. This will be a problem
    because all the image data will no longer be there.

    So my question is.. Is it possible for me to intercept this so that
    the drawRect will not be called? Or maybe there can be other
    workaround so that my App will not need to redraw the NSView
    unnecessarily?

    Thanks a lot for your attention.

    Regards,
    David
  • DrawRect will be called whenever at least a portion of a view is
    visible (e.g. if it WAS hidden by an overlapping window)
    Try not to intercept those calls but always honor draw requests.

    If you really draw the same very often, cache your drawing in an
    NSImage and then blit only that onscreen.

    Regards,
    Dominik

    Am Oct 30, 2006 um 8:28 AM schrieb David Chan:

    > Hi all, I have a question regarding NSView. I have an application that
    > keep many subclassed NSView objects. Users shall be able to navigate
    > around and depending on their  action, certain NSView objects will be
    > shown and other will be hidden or moved to other window
    >
    > Now.. I actually only need to use drawRect once for every object -
    > when I first created the objects. I will neither need to scroll nor do
    > anything that will require redrawing of the NSView objects. By right,
    > it should be safe to release all the image data objects I use after
    > the drawRect returns; and that is my intention in order to minimize
    > the memory usage. But it seems that there are certain occasions where
    > the Application would call drawRect on its own. This will be a problem
    > because all the image data will no longer be there.
    >
    > So my question is.. Is it possible for me to intercept this so that
    > the drawRect will not be called? Or maybe there can be other
    > workaround so that my App will not need to redraw the NSView
    > unnecessarily?
    >
    > Thanks a lot for your attention.
    >
    > Regards,
    > David
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<dominik...>
    >
    > This email sent to <dominik...>
    >
  • On Oct 30, 2006, at 2:21 AM, Dominik Pich wrote:

    > DrawRect will be called whenever at least a portion of a view is
    > visible (e.g. if it WAS hidden by an overlapping window)
    > Try not to intercept those calls but always honor draw requests.

    Exactly.

    > If you really draw the same very often, cache your drawing in an
    > NSImage and then blit only that onscreen.

    Or, since the original poster was concerned about memory usage, you
    can always have drawRect: do this:

    - (void)drawRect:(NSRect)aRect
    {
        // load all images here and build up drawing objects (colors,
    attributes for text, etc.)

        // do drawing

        // release images, drawing objects, etc.
    }

    This, of course, will incur a performance penalty.

    --
    > Am Oct 30, 2006 um 8:28 AM schrieb David Chan:
    >
    >> Hi all, I have a question regarding NSView. I have an application
    >> that
    >> keep many subclassed NSView objects. Users shall be able to navigate
    >> around and depending on their  action, certain NSView objects will be
    >> shown and other will be hidden or moved to other window
    >>
    >> Now.. I actually only need to use drawRect once for every object -
    >> when I first created the objects. I will neither need to scroll
    >> nor do
    >> anything that will require redrawing of the NSView objects. By right,
    >> it should be safe to release all the image data objects I use after
    >> the drawRect returns; and that is my intention in order to minimize
    >> the memory usage. But it seems that there are certain occasions where
    >> the Application would call drawRect on its own. This will be a
    >> problem
    >> because all the image data will no longer be there.
    >>
    >> So my question is.. Is it possible for me to intercept this so that
    >> the drawRect will not be called? Or maybe there can be other
    >> workaround so that my App will not need to redraw the NSView
    >> unnecessarily?

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • Thank you for the replies guys. I think reloading and releasing the
    data everytime drawRect is called will harm the performance and that
    will be undesirable. I will try to cache the drawing in NSImage and
    monitor its memory usage.

    Thanks..

    On 10/30/06, Ricky Sharp <rsharp...> wrote:
    >
    > On Oct 30, 2006, at 2:21 AM, Dominik Pich wrote:
    >
    >> DrawRect will be called whenever at least a portion of a view is
    >> visible (e.g. if it WAS hidden by an overlapping window)
    >> Try not to intercept those calls but always honor draw requests.
    >
    > Exactly.
    >
    >> If you really draw the same very often, cache your drawing in an
    >> NSImage and then blit only that onscreen.
    >
    > Or, since the original poster was concerned about memory usage, you
    > can always have drawRect: do this:
    >
    > - (void)drawRect:(NSRect)aRect
    > {
    > // load all images here and build up drawing objects (colors,
    > attributes for text, etc.)
    >
    > // do drawing
    >
    > // release images, drawing objects, etc.
    > }
    >
    > This, of course, will incur a performance penalty.
    >
    > --
    >> Am Oct 30, 2006 um 8:28 AM schrieb David Chan:
    >>
    >>> Hi all, I have a question regarding NSView. I have an application
    >>> that
    >>> keep many subclassed NSView objects. Users shall be able to navigate
    >>> around and depending on their  action, certain NSView objects will be
    >>> shown and other will be hidden or moved to other window
    >>>
    >>> Now.. I actually only need to use drawRect once for every object -
    >>> when I first created the objects. I will neither need to scroll
    >>> nor do
    >>> anything that will require redrawing of the NSView objects. By right,
    >>> it should be safe to release all the image data objects I use after
    >>> the drawRect returns; and that is my intention in order to minimize
    >>> the memory usage. But it seems that there are certain occasions where
    >>> the Application would call drawRect on its own. This will be a
    >>> problem
    >>> because all the image data will no longer be there.
    >>>
    >>> So my question is.. Is it possible for me to intercept this so that
    >>> the drawRect will not be called? Or maybe there can be other
    >>> workaround so that my App will not need to redraw the NSView
    >>> unnecessarily?
    >
    > ___________________________________________________________
    > Ricky A. Sharp        mailto:<rsharp...>
    > Instant Interactive(tm)  http://www.instantinteractive.com
    >
    >
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today