objective-ccocoawebkitikimageview

Image from WebView dragged onto IKImageView


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?


Solution

  • 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;
    }