Cannot drag image from my app to Desktop/Finder with SwiftUI `.onDrag` or `.draggable`

huangapple go评论74阅读模式
英文:

Cannot drag image from my app to Desktop/Finder with SwiftUI `.onDrag` or `.draggable`

问题

我正在在macOS 13.1上使用SwiftUI编写一个应用程序,并希望从我的应用程序中将图像拖动到桌面/查找器中。参考这篇帖子,我尝试了以下代码并使用了.onDrag。但无论我如何拖动以及在何处拖动,拖动都没有任何反应。

如果我将typeIdentifierUTType.image.identifier更改为UTType.tiff.identifier,那么我的应用程序中的图像至少可以成功地拖动到Apple Pages,但在拖动到桌面/查找器时仍然失败。

然后我尝试将.onDrag替换为.draggable(image.tiffRepresentation ?? Data()),但结果是一样的。

我该如何解决这个问题?

英文:

I'm writing an app in SwiftUI on macOS 13.1, and want to drag image from my app to Desktop/Finder. Referred to this post, I gave .onDrag a try with following code. But the dragging don't have any reaction no matter how and where I drag it.

Cannot drag image from my app to Desktop/Finder with SwiftUI `.onDrag` or `.draggable`

struct ContentView: View {
    
    @State var image: NSImage = NSImage(imageLiteralResourceName: "demoImage")
    
    var body: some View {
        Image(nsImage: image)
            .resizable()
            .scaledToFit()
            .frame(width: 200, height: 200)
            .onDrag {
                NSItemProvider(item: image.tiffRepresentation as NSSecureCoding?, typeIdentifier: UTType.image.identifier)
            }
    }
}

If I change typeIdentifier from UTType.image.identifier to UTType.tiff.identifier, the image in my app can at least successfully be dragged to Apple Pages, but it still fails when dragging to Desktop/Finder.

Then I tried to replace .onDrag with .draggable(image.tiffRepresentation ?? Data()), but the outcome is in the same.

How can I solve this?

答案1

得分: 1

受到@Ivan Ičin答案的启发,我成功地得到了这段代码。在macOS 13.1上进行了测试

Image(nsImage: image)
    .resizable()
    .scaledToFit()
    .frame(width: 200, height: 200)
    .onDrag {
        let fileURL = FileManager.default.homeDirectoryForCurrentUser.appending(component: "cover.png")
        // 我扩展了`URL`,请随意以你自己的方式创建文件
        fileURL.createFile(contents: image.pngData)
                    
        return NSItemProvider(item: fileURL as NSSecureCoding, typeIdentifier: UTType.fileURL.identifier)
    }
英文:

Inspired by @Ivan Ičin 's answer, I managed to have this code. Tested on macOS 13.1

Image(nsImage: image)
    .resizable()
    .scaledToFit()
    .frame(width: 200, height: 200)
    .onDrag {
        let fileURL = FileManager.default.homeDirectoryForCurrentUser.appending(component: "cover.png")
        // I extended `URL`, feel free to create file in your own way
        fileURL.createFile(contents: image.pngData)
                    
        return NSItemProvider(item: fileURL as NSSecureCoding, typeIdentifier: UTType.fileURL.identifier)
    }

答案2

得分: 0

Finder只接受文件和文件夹(以及如果您想执行更复杂操作,则接受文件承诺)。因此,最简单的方法是将图像保存在磁盘上并发送URL。

英文:

The Finder does accept only files and folders (and file promises if you want to do something more complicated). As such the simplest thing would be to save the image on the disk and send the URL.

huangapple
  • 本文由 发表于 2023年2月10日 15:45:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定