Pasting NSImage problems

  • All right. I have a subclass of NSImageView. I want to be able to
    have it accept the paste operation when there is an image on the
    pasteboard. That part is easy enough. However when the paste function
    in the subclass is called nothing draws. What I am hoping for is that
    the image on the pasteboard will draw on top of the image in the view.
    (Making it movable and such is a nightmare I'll deal with when I get
    there.)

    Here is the code I thought would work:

    -(IBAction)paste:(id)sender
    {
    NSPasteboard *pboard = [NSPasteboard generalPasteboard];
    NSImage * newImage = [[NSImage alloc]initWithData:[pboard
    dataForType:NSTIFFPboardType]];
    NSSize is =[newImage size];

    NSRect imageRect = NSMakeRect(100.0,100.0,is.width,is.height);
      NSBezierPath
      *croppingPath = [NSBezierPath bezierPathWithRect:imageRect];

    [[self image] lockFocus];
    [[NSColor blackColor]set];
    [croppingPath fill];
    [newImage compositeToPoint:NSZeroPoint fromRect:imageRect
    operation:NSCompositeSourceIn];
    [[self image] unlockFocus];

    }

    I have tried a number of different composite operations but the
    results are the same. I've spent the last couple of hours on Google,
    the mailing list archive,  and the Dev site and if the answer was
    there I missed it. I also looked over my programming books with no
    luck. I thought that the code from the example sketch would help but
    it did not. So I'm missing something somewhere and hope some one out
    there can help me.

    Dont know if you noticed or not but this whole drawing thing is
    driving me nuts. Every time I think I understand it I run in to
    something I cant wrap my head around.
  • Just subclass NSView directly, add an instance
    variable or two to store the images you want to draw,
    and implement -drawRect to draw them.  NSImageView
    isn't doing anything magic, and it sounds like what
    you want is sufficiently different from NSImageView
    that a there is no point in starting with NSImageView.

    If you are drawing multiple images or want to drag
    images, you might as well jump in and learn how to do
    it.  NSImageView is a simple class for use in simple
    situations.  NSView is the general purpose base class
    for drawing and responding to user input.
  • Hmm, I don't think I explained correctly then. I HAVE subclassed
    NSImageView to add in all the things I need it to do such as rotation
    and cropping (which work). I thought that the code I had posted was
    interacting directly with the image, and then only asking the
    imageview to display it. Where my code is failing is that Although it
    picks the image up off the clipboard correctly, I cannot get it to
    draw the pasted image. Now if I simply pull the image off the
    clipboard and tell the custom view setImage: then the image displays
    but the image it should be pasted on is of course replaced.

    On Oct 14, 2007, at 8:19 PM, Erik Buck wrote:

    > Just subclass NSView directly, add an instance
    > variable or two to store the images you want to draw,
    > and implement -drawRect to draw them.  NSImageView
    > isn't doing anything magic, and it sounds like what
    > you want is sufficiently different from NSImageView
    > that a there is no point in starting with NSImageView.
    >
    > If you are drawing multiple images or want to drag
    > images, you might as well jump in and learn how to do
    > it.  NSImageView is a simple class for use in simple
    > situations.  NSView is the general purpose base class
    > for drawing and responding to user input.
    >
    >
  • On Oct 14, 2007, at 6:12 PM, Development wrote:

    > [newImage compositeToPoint:NSZeroPoint fromRect:imageRect
    > operation:NSCompositeSourceIn];

    The actual compositing should only happen in drawRect (or a method
    drawRect calls directly).

    Paste is only called once, but your view may be asked to draw its
    image very rapidly over and over (maybe 15 times per second, for
    example). If the image only exists in the paste method, it will never
    stay around long enough to make it to the screen.

    The only thing the paste method should do is capture the image data
    from the pasteboard and set the image object as an instance variable.
    The idea is to keep it around for when it needs to be drawn in drawRect.

    This stuff is covered in this tutorial:

    "Cocoa Graphics with Quartz II"
    <http://cocoadevcentral.com/d/intro_to_quartz_two/>

    Scroll down to "Create and Display Images," but make sure to read the
    rest of the tutorial as well, because it tells you how to keep image
    data in memory.

    The only difference for your situation is that you're getting the
    data from the pasteboard instead of from a file on disk, but
    everything else is essentially the same. There's a sample project
    available for download at the bottom of the page.

    For the dragging part, you can look at this:

    "THCanvasView 1.0.1 and PhotoStacks Sample App"
    <http://theocacao.com/document.page/503>

        - Scott
  • The following code adds the pb image to the existing one at the
    zeropoint. I'll make it movable later, but this is the fix I managed
    after many hours

    -(IBAction)paste:(id)sender
    {
    NSPasteboard *pboard = [NSPasteboard generalPasteboard];
    NSImage * newImage = [[NSImage alloc]initWithData:[pboard
    dataForType:NSTIFFPboardType]];
    NSImage * src = [[NSImage alloc]initWithData:[[self image]
    TIFFRepresentation]];
    NSSize is =[newImage size];
    NSRect imageRect = NSMakeRect(0,0,is.width,is.height);
    NSLog(@"Image: %f,%f %f,%
    f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.
    size.height);
    NSBezierPath * croppingPath = [NSBezierPath
    bezierPathWithRect:imageRect];

    [src lockFocus];
    [[NSColor blackColor]set];
    [croppingPath fill];
    [newImage compositeToPoint:NSZeroPoint
    operation:NSCompositeSourceOver];
    [src unlockFocus];
    [self setImage:src];
    }

    It's a little laggy but will function... of course the image I am
    working with is a raw 2000px X 2000px Tiff so I expect a little slowness
previous month october 2007 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