Professional Web Applications Themes

NSImage locking focus - Mac Programming

Cocoa/objective-c question. I haven't quite nailed down exactly what's happening, so bare with me. I have an image that'll be displayed in an NSImageView. I can lockFocus on it, draw into it, re-display the view, etc. Everything's peachy. Later on in the program, I need to relock focus onto that same image, alter it, and then re-display the view. No problems. But, when the image gets above a certain threshold, I can't successfully lockFocus and draw into it the second time. The thresholds that I'm seeing are ridiculously small - a 480 pixel wide image is enough to choke (100 ...

  1. #1

    Default NSImage locking focus

    Cocoa/objective-c question.

    I haven't quite nailed down exactly what's happening, so bare with me.

    I have an image that'll be displayed in an NSImageView. I can lockFocus
    on it, draw into it, re-display the view, etc. Everything's peachy.

    Later on in the program, I need to relock focus onto that same image,
    alter it, and then re-display the view. No problems.

    But, when the image gets above a certain threshold, I can't successfully
    lockFocus and draw into it the second time. The thresholds that I'm
    seeing are ridiculously small - a 480 pixel wide image is enough to
    choke (100 pixels tall). The first lock and draw succeeds, subsequent
    ones fail.

    The code all performs fine for smaller images (say, 400 pixels x 400
    pixels). In those cases, I get the first draw, it does its calculations,
    then I get the second draw.

    With these slightly wider (or taller) images, I only successfully get
    the first draw.

    I monkeyed around with it for about 2 hours and finally hit upon a
    kludge - if I copy the image referenced in the NSImageView, I can
    lockFocus on that, draw into it, set it as the image for the imageView,
    re-display, and everything's fine.

    Considering the small size of the images in this case, it's not a big
    concern to duplicate it. Still, it's quite peculiar to me and I wouldn't
    want to have this as my only fix if I ended up using much larger images
    than I have now.

    I get no exceptions, no failures, no errors, no nothing. It all appears
    to succeed, I just don't get the updated NSImageView.

    Incidentally, I tested a bit more and determined that I can copy the
    image after the draw, re-set it, and it's fine. So it appears that my
    drawing is working fine, it's just my NSImageView that doesn't always
    want to update itself. I'm just sending it a display message, but I've
    also tried displayInRect, displayIfNeeded, and displayIfNeededInRect.
    Flushing the window doesn't help either.

    Is there any other trick I can try to force the NSImageView to
    re-display? Again - it only seems to fail with images
    slightly-larger-than-normal.

    I feel like I'm missing something obvious.

    Ideas?

    -Jim.....
    Jim Guest

  2. #2

    Default Re: NSImage locking focus

    On Thu, 1 Apr 2004, Jim Thomason wrote:
     

    A similar problem was covered in the group a bit back. The solution was
    that NSImageView is a bit too aggressive in its caching, so you have to
    convince it to reload. I assume that the size difference is triggering the
    caching. You have to do something like this to make it reload:

    [imageview setImage:nil];
    [imageview setImage:image];

    Don't forget to retain the image first if you aren't already.
    Michael Guest

  3. #3

    Default Re: NSImage locking focus

    Jim Thomason wrote:
     

    Not particularly obvious, but easily remedied.

    NSImageView will create a cached image if it has to scale the image
    you've set. NSImage won't know that you've changed the image, because
    all it knows is that it has the same object you handed it before in a
    -setImage: message.

    Try this:

    implementation NSImage (dumpTheCache)

    - (void) recacheImage
    {
    NSImage *image = [[self image] retain];
    [self setImage:nil];
    [self setImage:image];
    [image release];
    }

    end

    HTH,

    -jcr
    John Guest

  4. #4

    Default Re: NSImage locking focus

    "John C. Randolph" wrote: 
    >
    > Not particularly obvious, but easily remedied.
    >
    > NSImageView will create a cached image if it has to scale the image
    > you've set. NSImage won't know that you've changed the image, because
    > all it knows is that it has the same object you handed it before in a
    > -setImage: message.
    >
    > Try this:
    >
    > implementation NSImage (dumpTheCache)[/ref]


    Oops, make that:

    implementation NSImageView (dumpTheCache)
     
    John Guest

Similar Threads

  1. Need Help with Locking
    By CFRAM in forum Coldfusion - Advanced Techniques
    Replies: 6
    Last Post: July 28th, 03:16 AM
  2. Popup; show, take focus, wait for button action, give focus, hide
    By John van Terheijden in forum Macromedia Flash
    Replies: 0
    Last Post: February 2nd, 12:08 PM
  3. NSImage and transparency
    By Tord in forum Mac Programming
    Replies: 5
    Last Post: December 15th, 06:16 PM
  4. focus/lost focus of custom control
    By flash in forum ASP Components
    Replies: 1
    Last Post: November 21st, 03:18 PM
  5. Replies: 5
    Last Post: September 25th, 09:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139