Professional Web Applications Themes

NSImageView and reloadData - Mac Programming

Hello, I have a NSImageView. With this Code I get the raw pixels of the image of the NSImageView: int x,y; // pixel coordinates int width,height; NSSize size; NSImage *img = [imageView image]; // the image NSArray *reps=[img representations]; NSEnumerator *enumerator = [reps objectEnumerator]; id obj; NSBitmapImageRep *bir; [progressIndicator startAnimation:self]; // cycle through the image reps until we find a suitable one while ((obj = [enumerator nextObject])) { bir=nil; if([obj isKindOfClass: [NSBitmapImageRep class]]) { bir=(NSBitmapImageRep *)obj; // we select a 32 bpp RGB image with 8 bit per sample if([bir bitsPerPixel]==32 && ![bir isPlanar] && [bir samplesPerPixel]==3 && [bir colorSpaceName]==NSCalibratedRGBColorSpace) ...

  1. #1

    Default NSImageView and reloadData

    Hello,

    I have a NSImageView. With this Code I get the raw pixels of the image
    of the NSImageView:

    int x,y; // pixel coordinates
    int width,height;
    NSSize size;
    NSImage *img = [imageView image]; // the image

    NSArray *reps=[img representations];
    NSEnumerator *enumerator = [reps objectEnumerator];
    id obj;
    NSBitmapImageRep *bir;

    [progressIndicator startAnimation:self];

    // cycle through the image reps until we find a suitable one
    while ((obj = [enumerator nextObject])) {
    bir=nil;
    if([obj isKindOfClass: [NSBitmapImageRep class]]) {
    bir=(NSBitmapImageRep *)obj;
    // we select a 32 bpp RGB image with 8 bit per sample
    if([bir bitsPerPixel]==32 &&
    ![bir isPlanar] &&
    [bir samplesPerPixel]==3 &&
    [bir colorSpaceName]==NSCalibratedRGBColorSpace)
    break;
    }
    }

    if(bir) {
    NSLog("Found bir");
    size = [bir size];
    width = size.width;
    height = size.height;

    struct {
    unsigned char r,g,b,a;
    } *pixels=(void*) [bir bitmapData];
    unsigned int ppl=[bir bytesPerRow]/4;
    // assuming rows are aligned...
    for(y=0; y<width; y++) {
    for(x=0; x<height; x++) {
    // set the pixel pointed by x, y to yellow
    pixels[x+ppl*y].r=0xff;
    pixels[x+ppl*y].g=0xff;
    pixels[x+ppl*y].b=0;
    }
    }
    }
    else {
    // error: no suitable image rep
    }

    (<http://www.faqts.com/knowledge_base/view.phtml/aid/16640/fid/1088>)

    The code runs without a problem but after the procession the image in
    the NSImageView isn't changed. The view shows the same image like before
    the function.

    But there is no [imageView reloadData] What can I do to update the view?

    Thanks - Robert
    Robert Guest

  2. #2

    Default Re: NSImageView and reloadData

    "Robert W. Kuhn" wrote: 

    You're running into an efficiency measure that NSImageView does. Once
    it's generated a rendering of the image at the screen resolution, it
    will just keep drawing that until it gets a new image to draw. All it
    does to see if the image is new is check whether the pointer it has has
    changed. It doesn't look through that pointer to see if the underlying
    image data is different.
     

    You'll need to get rid of the imageView's cached image and then send it
    a -setNeedsDisplay:YES message. You can probably do this my setting the
    imageView's image to nil, and then setting it to your modified image
    immediately, like so:

    interface NSImage (reload)

    - reloadImage;

    end
    implementation NSImage (reload)

    - reloadImage
    {
    id stashImage = [[self image] retain];
    [self setImage:nil];
    [self setImage:stashImage];
    [stashImage release];
    [self setNeedsDisplay:YES];
    }

    end


    It might also work if you just did [myImageView setImage:[myImageView
    image]], although it might be too clever for that.

    HTH,

    -jcr
    John Guest

  3. #3

    Default Re: NSImageView and reloadData

    John C. Randolph <idiom.com> wrote:
     

    Thank you, this is the solution!

    Tschau - Robert
    --
    vertrau
    voraus voraus
    Robert Guest

  4. #4

    Default Re: NSImageView and reloadData

    "Robert W. Kuhn" wrote: 
    >
    > Thank you, this is the solution![/ref]

    BTW, please go to bugreporter.apple.com and file a feature request for
    an API to do this.

    -jcr
    John Guest

  5. #5

    Default Re: NSImageView and reloadData

    John C. Randolph <idiom.com> wrote:
     

    [x] Done
    ID# :3555961.

    Tschau - Robert
    --
    vertrau
    voraus voraus
    Robert Guest

  6. #6

    Default Re: NSImageView and reloadData

    "John C. Randolph" <idiom.com> wrote in message news:<idiom.com>... 
    > >
    > > Thank you, this is the solution![/ref]
    >
    > BTW, please go to bugreporter.apple.com and file a feature request for
    > an API to do this.
    >
    > -jcr[/ref]

    To be consistent, you should be asking for an additionial
    NSMutableImageView Class instead of an additional method.
    jpa Guest

  7. #7

    Default Re: NSImageView and reloadData

    In article <google.com>,
    be (jpa) wrote:
     
    > >
    > > BTW, please go to bugreporter.apple.com and file a feature request for
    > > an API to do this.
    > >
    > > -jcr[/ref]
    >
    > To be consistent, you should be asking for an additionial
    > NSMutableImageView Class instead of an additional method.[/ref]

    That wouldn't make very much sense. The mutable/immutable distinction
    only exists for Foundation 'model' classes. Also, NSImageView is
    *already* mutable, it's just being overzealous in its caching.
    Michael Guest

Similar Threads

  1. Replies: 4
    Last Post: September 18th, 06:28 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