首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拖放NSImage

拖放NSImage
EN

Stack Overflow用户
提问于 2015-05-17 15:07:11
回答 1查看 895关注 0票数 2

我正在制作一个应用程序,我希望能够将图像拖到特定的第三方应用程序Anki中。图像是从互联网上加载的,所以硬盘上不存在。API文档似乎提供了三种这样做的方法(注意,我故意忽略了使用现在不再推荐的API函数的替代方法):

  • 方法1-方法1与Anki和其他一些应用程序一起工作。但是,它涉及到在桌面上创建一个临时文件。这并不理想。
  • 方法2-我根本无法让方法2工作。代码编译并运行,没有错误,但是拖放映像似乎什么也不做,不管我在哪个应用程序上删除图像。
  • 方法3-方法3适用于某些应用程序。例如,我可以将图像拖到finder中的文件夹中,它创建了我删除它的图像。但是,Anki不会以这种方式接受文件。

所以,我的问题是第二部分:

方法2可以工作吗?-这将避免创建不必要的临时文件,因此将是理想的。NSImage支持NSPasteboardWriter,所以它似乎应该能工作。但是,我找不到一个如何去做的例子。

--如果我必须使用方法#1,建议在哪里放置这样的临时文件?如果我必须创建一个临时文件,以便将图像放到目标应用程序中,那么建议将这些文件放在哪里?

代码语言:javascript
复制
//
//  DraggableImageView.swift
//  Lang Search
//
//  Created by Kevin Yancey on 11/21/14.
//  Copyright (c) 2014 Kevin Yancey. All rights reserved.
//

import Foundation
import Cocoa
import AppKit

class DraggableImageView : NSImageView, NSDraggingSource {
    //Method #1 - It works, but isn't ideal yet...
    override func mouseDragged(theEvent: NSEvent) {
        if let image = self.image {
            let imgRep = image.representations[0] as! NSBitmapImageRep;
            let data = imgRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: NSDictionary() as [NSObject : AnyObject])!;
            data.writeToFile("/Users/kpyancey/Desktop/copiedImage.png", atomically: false);
            let draggingItem : NSDraggingItem = NSDraggingItem(pasteboardWriter: NSURL.fileURLWithPath("/Users/kpyancey/Desktop/copiedImage.png")!)
            let draggingItems : [AnyObject] = [draggingItem]
            let draggingSession = beginDraggingSessionWithItems(draggingItems, event: theEvent, source: self)
        }
    }

    //Method #2 - doesn't seem to work at all
    /*override func mouseDragged(theEvent: NSEvent) {
        if let image = self.image {
            let draggingItem : NSDraggingItem = NSDraggingItem(pasteboardWriter: image)
            let draggingItems : [AnyObject] = [draggingItem]
            let draggingSession = beginDraggingSessionWithItems(draggingItems, event: theEvent, source: self)
        }
    }*/
    //Method #3 - works, but not with the target application (Anki)
    /*
    override func mouseDragged(theEvent: NSEvent) {
        if let image = self.image {
            let draggingItem : NSDraggingItem = NSDraggingItem(pasteboardWriter: image)
            let draggingItems : [AnyObject] = [draggingItem]
            let draggingSession = beginDraggingSessionWithItems(draggingItems, event: theEvent, source: self)
        }
    }*/
    /*
    override func mouseDragged(theEvent: NSEvent) {
        var dragPosition = convertPoint(theEvent.locationInWindow, fromView: nil)
        dragPosition.x -= 16
        dragPosition.y -= 16
        let imageLocation = NSRect(origin: dragPosition, size: NSMakeSize(32, 32))
        self.dragPromisedFilesOfTypes(["png"], fromRect: imageLocation, source: self, slideBack: true, event: theEvent)
    }

    override func namesOfPromisedFilesDroppedAtDestination(dropDestination: NSURL) -> [AnyObject]? {
        let imgRep = image!.representations[0] as! NSBitmapImageRep;
        let data = imgRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: NSDictionary() as [NSObject : AnyObject])!;
        let filePath = dropDestination.URLByAppendingPathComponent("test.png")
        data.writeToURL(filePath, atomically: false);
        return [filePath]
    }*/

    func draggingSession(session: NSDraggingSession, sourceOperationMaskForDraggingContext context: NSDraggingContext) -> NSDragOperation {
        return NSDragOperation.Copy
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-17 22:21:13

很明显Anki想在黑板上放个url。Method#1很棒,但是在桌面上使用NSCachesDirectory而不是硬编码:

代码语言:javascript
复制
NSURL* osURL = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil] URLByAppendingPathComponent:@"copiedImage.png"]; 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30288296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档