Note that there are some explanatory texts on larger screens.

plurals
  1. POdrag and drop: NSView thinks source is an image that it isn't
    primarykey
    data
    text
    <p>I have two views. 1. One view has a grid of NSImageViews with images. 2. The other view has a grid of potential places to place the dragged image.</p> <p>I think I have dragging working. If I click and drag an image from view 1, it works fine, but when I try to place it, the path of the source image is for an image on my desktop that isn't even part of my project. The result of the below code is that carriedData = nil, but I did some testing with NSLog and saw that it is sending the path for an image outside the project that is no my desktop.</p> <p>Here is some of the D&amp;D protocol methods I have implemented. myDragState is just en enumeration I use for highlighting the grid space that the drag is currently hovering over.</p> <p>in Init...</p> <pre><code>[self registerForDraggedTypes:[NSImage imagePasteboardTypes]]; myDragState = dragStateNone; </code></pre> <p>Then the protocol methods</p> <pre><code>- (NSDragOperation)draggingEntered:(id &lt;NSDraggingInfo&gt;)sender { if ((NSDragOperationGeneric &amp; [sender draggingSourceOperationMask]) == NSDragOperationGeneric) { myDragState = dragStateOver; [self setNeedsDisplay]; return NSDragOperationGeneric; } else { return NSDragOperationNone; } } - (void)draggingExited:(id &lt;NSDraggingInfo&gt;)sender { //turn off focus ring myDragState = dragStateExited; [self setNeedsDisplay]; } - (void)draggingEnded:(id &lt;NSDraggingInfo&gt;)sender { } - (BOOL)prepareForDragOperation:(id &lt;NSDraggingInfo&gt;)sender { return YES; } - (BOOL)performDragOperation:(id &lt;NSDraggingInfo&gt;)sender { NSPasteboard *paste = [sender draggingPasteboard]; //gets the dragging-specific pasteboard from the sender NSArray *types = [NSArray arrayWithObjects:NSTIFFPboardType, nil]; //a list of types that we can accept NSString *desiredType = [paste availableTypeFromArray:types]; NSData *carriedData = [paste dataForType:desiredType]; if (nil == carriedData) { //the operation failed for some reason NSRunAlertPanel(@"Paste Error", @"Sorry, but the past operation failed", nil, nil, nil); myDragState = dragStateNone; [self setNeedsDisplay]; return NO; } else { //the pasteboard was able to give us some meaningful data if ([desiredType isEqualToString:NSTIFFPboardType]) { NSLog(@"TIFF"); //we have TIFF bitmap data in the NSData object NSImage *newImage = [[NSImage alloc] initWithData:carriedData]; [self setImage:newImage]; myDragState = dragStateSet; } else { //this can't happen //NSAssert(NO, @"This can't happen"); NSLog(@"Other type"); myDragState = dragStateNone; [self setNeedsDisplay]; return NO; } } //redraw us with the new image return YES; } - (void)concludeDragOperation:(id &lt;NSDraggingInfo&gt;)sender { //re-draw the view with our new data [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. NSRect ourBounds = [self bounds]; if (myDragState == dragStateSet) { NSImage *image = [self image]; [super drawRect:dirtyRect]; [image compositeToPoint:(ourBounds.origin) operation:NSCompositeSourceOver]; } else if (myDragState == dragStateOver) { [[NSColor colorWithDeviceRed:1.0f green:1.0f blue:1.0f alpha:0.4f] set]; [NSBezierPath fillRect:ourBounds]; } else { //draw nothing } } </code></pre> <p>Edit: So I figured this out. The problem was actually with the source. I wasn't copying it to the PBoard properly. My code for that is: </p> <pre><code>NSPasteboard *zPasteBoard = [NSPasteboard pasteboardWithName:NSDragPboard]; [zPasteBoard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:self]; [zPasteBoard setData:[tileImageView.image TIFFRepresentation] forType:NSTIFFPboardType]; </code></pre> <p>Now I am getting some weird effect when the images show though. As the images are placed in the destination imageview, they are being resized, but the larger draggable version is also still showing up. So, I am getting two images on the image view.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload