1

I have WebView where I load content of webarchive. In the same view I have IKImageView outlet. Image drag n drop from web view onto image view doesn't work for me.

What is weird, it works when I drag photo e.g. from iPhoto onto the same image view. Also, I can drag image from my web view onto NSScrollView (which creates a link to the image) and I can drag the same photo onto a new Mail message (created an image as expected).

IKImageView has "Supports Drag and Drop" enabled in the IB.

What am I missing here?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62
  • “Image drag n drop from web view onto image view doesn't work for me.” Please be more specific. What happens instead? – Peter Hosey Dec 18 '09 at 23:25
  • I can drag an image (which turns to thumbnail and is shadowed out) but hovering over IKImageView does not switch cursor to the one with green plus sign and releasing the mouse button causes my dragged image to "go back" to the web view (to the original image place). – Piotr Byzia Dec 19 '09 at 10:59

2 Answers2

1

IKImageView is probably expecting a pasteboard of NSFilenamesPboardType, how does the webview handle dragging images?

catsby
  • 11,276
  • 3
  • 37
  • 37
0

It turned out, that the best way to handle d'n'd in my case is via WebArchivePboardType. Then:

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;

    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];

    // Create image data from webarchive stored in a pasteboard.    
    NSData *image = [pboard dataForType:WebArchivePboardType];
    WebArchive *webArchive = [[WebArchive alloc] initWithData:image];

    // Let's see what are we dragging.
    for (WebResource *subresource in [webArchive subresources])
    {
        NSString *mimeType = [subresource MIMEType];
        if ([mimeType hasPrefix:expectedMimeTypeStartsWith])
        {
            NSData *data = [subresource data];

            CFDataRef imgData = (CFDataRef)data;
            CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData (imgData);

            CGImageRef image;

            if ([mimeType hasSuffix:@"png"])
            {
                image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);   
            }
            else if ([mimeType hasSuffix:@"jpeg"])
            {
                image = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
            }

            [self setImage:image imageProperties:nil];
        }
    }
    return YES;
}
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62