【发布时间】:2015-05-18 06:21:13
【问题描述】:
我正在制作一个应用程序,我希望能够从中将图像拖到特定的 3rd 方应用程序 Anki 中。该图像是从 Internet 加载的,因此不存在于硬盘驱动器上。 API 文档似乎提供了三种执行此操作的方法(请注意,我故意省略了使用现已弃用的 API 函数的替代方法):
方法 #1 - 方法 #1 适用于 Anki 和其他一些应用程序。 但是,它涉及在桌面上创建一个临时文件。这个 并不理想。
方法 #2 - 我根本无法让方法 #2 工作。代码编译并运行 没有错误,但无论如何拖动图像似乎什么都不做 我将图像放在哪个应用程序上。
方法 #3 - 方法 #3 适用于某些应用程序。例如,我可以拖动 图像进入finder中的文件夹,它会在我创建的图像 把它丢了。但是,Anki 不会以这种方式接受文件。
所以,我的问题分为两部分:
方法#2 可以工作吗? 这样可以避免创建不必要的临时文件,因此非常理想。 NSImage 支持 NSPasteboardWriter,所以它看起来应该可以工作。但是,我找不到如何做到这一点的示例。
如果我必须使用方法 #1,建议将这些临时文件放在哪里?如果我必须创建一个临时文件才能将图像放入目标应用程序,那么在哪里是否推荐放置此类文件的位置?
//
// 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
}
}
【问题讨论】: