Converting NSView to NSImage

  • I'm trying to convert an NSView to an NSImage to be displayed in an
    NSImageView:

    NSImage * clipping = [[NSImage alloc] initWithData:[[myWindow
    contentView] dataWithPDFInsideRect:[myWindow frame]]];
    [myImageView setImage:clipping];
    [clipping release];

    The above doesn't work, what the right way to do this?
  • On Wednesday, May 30, 2001, at 03:40 PM, Mike Vannorsdel wrote:

    > I'm trying to convert an NSView to an NSImage to be displayed in an
    > NSImageView:
    >
    > NSImage * clipping = [[NSImage alloc] initWithData:[[myWindow
    > contentView] dataWithPDFInsideRect:[myWindow frame]]];
    > [myImageView setImage:clipping];
    > [clipping release];

    I think you should be using the bounds of the view rather than the frame
    of the window.

    NSImage * clipping = [[NSImage alloc] initWithData:[[myWindow
    contentView] dataWithPDFInsideRect:[[myWindow contentView]  bounds]]];
    [clipping setDataRetained:YES]; // Tends to make re-scaling work better.
    [myImageView setImage:clipping];
    [clipping release];

    > The above doesn't work, what the right way to do this?

    If a bitmap is OK, take a look at NSBimapImageRep's
    - (id)initWithFocusedViewRect:(NSRect)rect;

    And searching the Cocoa-dev list gives this for capture:
    NSImage* image;
    NSBitmapImageRep* rep;
    [view lockFocus];
    rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view bounds]];
    [view unlockFocus];
    image = [[[NSImage alloc] initWithSize:NSZeroSize] autorelease];
    [image addRepresentation:rep];
    [rep release];

    [myImageView setImage: image];

    Regards,
    John Hornkvist
    --
    ToastedMarshmallow, the perfect Cocoa companion
    http://www.toastedmarshmallow.com
  • Thanks a lot.  The bitmap image rep worked much better.

    On Wednesday, May 30, 2001, at 08:04  AM, John H√∂rnkvist wrote:

    >
    > On Wednesday, May 30, 2001, at 03:40 PM, Mike Vannorsdel wrote:
    >
    >
    > I think you should be using the bounds of the view rather than the
    > frame of the window.
    >
    > NSImage * clipping = [[NSImage alloc] initWithData:[[myWindow
    > contentView] dataWithPDFInsideRect:[[myWindow contentView]  bounds]]];
    > [clipping setDataRetained:YES]; // Tends to make re-scaling work better.
    > [myImageView setImage:clipping];
    > [clipping release];
previous month may 2001 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