我正在制作一个应用程序,我希望能够将图像拖到特定的第三方应用程序Anki中。图像是从互联网上加载的,所以硬盘上不存在。API文档似乎提供了三种这样做的方法(注意,我故意忽略了使用现在不再推荐的API函数的替代方法):
所以,我的问题是第二部分:
方法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
}
}发布于 2015-05-17 22:21:13
很明显Anki想在黑板上放个url。Method#1很棒,但是在桌面上使用NSCachesDirectory而不是硬编码:
NSURL* osURL = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil] URLByAppendingPathComponent:@"copiedImage.png"]; https://stackoverflow.com/questions/30288296
复制相似问题