SwiftUI - onDrag 参数类型 'NSImage' 不符合预期类型 'NSItemProviderWriting'
SwiftUI - onDrag Argument type 'NSImage' does not conform to expected type 'NSItemProviderWriting'
我正在尝试多种方法在 macOS 中实现图像拖动(从我的应用程序到其他应用程序),但其中 none 有效。
图像是从剪贴板中获取的 Data() 对象,而不是 URL.
我的代码:
.onDrag {
return NSItemProvider(object: NSImage(data: self.item.value) ?? NSImage())
}
它说
Argument type 'NSImage' does not conform to expected type
'NSItemProviderWriting'
我尝试使用文本,它可以正常工作。但是找不到拖动图片的方法。
以下是从测试 SwiftUI 应用程序到 TextEdit 的拖放操作。测试图像 image 存储在 Assets.xcassets
Image("image")
.onDrag {
NSItemProvider(item: NSImage(named: "image")?.tiffRepresentation as NSSecureCoding?,
typeIdentifier: kUTTypeTIFF as String)
}
我正在尝试多种方法在 macOS 中实现图像拖动(从我的应用程序到其他应用程序),但其中 none 有效。 图像是从剪贴板中获取的 Data() 对象,而不是 URL.
我的代码:
.onDrag {
return NSItemProvider(object: NSImage(data: self.item.value) ?? NSImage())
}
它说
Argument type 'NSImage' does not conform to expected type 'NSItemProviderWriting'
我尝试使用文本,它可以正常工作。但是找不到拖动图片的方法。
以下是从测试 SwiftUI 应用程序到 TextEdit 的拖放操作。测试图像 image 存储在 Assets.xcassets
Image("image")
.onDrag {
NSItemProvider(item: NSImage(named: "image")?.tiffRepresentation as NSSecureCoding?,
typeIdentifier: kUTTypeTIFF as String)
}