代码之家  ›  专栏  ›  技术社区  ›  Piotr Byzia

将WebView中的图像拖到IKImageView上

  •  1
  • Piotr Byzia  · 技术社区  · 16 年前

    我有 网络视图 我在这里加载webarchive的内容。我也有同样的看法 出口图像从web视图拖放到图像视图对我不起作用。

    此外,我还可以将图像从web视图拖到 NSScrollView (这会创建一个指向图像的链接),我可以将同一张照片拖到新邮件消息上(按预期创建图像)。

    我错过了什么?

    2 回复  |  直到 16 年前
        1
  •  1
  •   catsby    16 年前

    IKImageView 可能是在期待一个 NSFilenamesPboardType ,webview如何处理拖动图像?

        2
  •  0
  •   Piotr Byzia    16 年前

    事实证明,在我的情况下,处理d'n'd最好的方法是通过 WebArchivePboardType .

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