3

I'm trying several ways to implement image dragging (from my app to other apps) in macOS but none of them is working. The image is a Data() object, which was taken from the clipboard, not an URL.

My code:

.onDrag {
    return NSItemProvider(object: NSImage(data: self.item.value) ?? NSImage())
}

It says

Argument type 'NSImage' does not conform to expected type 'NSItemProviderWriting'

I tried with text and it's working. But can't find a way to drag an image.

Simon Pham
  • 1,603
  • 1
  • 10
  • 15
  • @Asperi it's not really helpful because what I am doing is implement dragging an image from my app to other apps. This tutorial is just about dragging to move the view around inside an app. – Simon Pham Mar 24 '20 at 05:11
  • 1
    I would cache somewhere `self.item.value` data as file and pass in `NSItemProvider` a URL to that stored image as `kUTTypeURL` type - it would be acceptable by much more destinations, including search box. – Asperi Mar 24 '20 at 09:24

1 Answers1

3

The following works as Drag&Drop from testing SwiftUI app to TextEdit. Testing image image is stored in Assets.xcassets

Image("image")
    .onDrag {
        NSItemProvider(item: NSImage(named: "image")?.tiffRepresentation as NSSecureCoding?, 
                       typeIdentifier: kUTTypeTIFF as String)
    }
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • I tried with my case but it doesn't work. I cannot drag the image from the app to Google Search Images or TextEdit. The NSImage() is not null. I can display it. return NSItemProvider(item: NSImage(data: self.historyItem.value)?.tiffRepresentation as NSSecureCoding?, typeIdentifier: kUTTypeTIFF as String). Edited: It cannot be drag. – Simon Pham Mar 24 '20 at 08:24
  • 1
    I've tested with Xcode 11.4 / macOS 10.15.3 - D&D in general works, so some not OK in your data. What is `self.historyItem.value`? BTW, have you tried to drag till the end, by default there is no preview in source, so it appears only over TextEdit. – Asperi Mar 24 '20 at 08:35
  • self.historyItem.value is a Data() object which taken from pasteboardItem.data(forType: .tiff). it's not about the data. I've tried with image stored in Assets.xcassets but it doesn't work. I use Xcode 11.2.1 / macOS 10.15.2. Maybe I should update my OS & Xcode. – Simon Pham Mar 24 '20 at 08:39
  • It's so strange. I cannot drag anything. Then rerun again, it can be drag. But I when I drop the image it doesn't send any data to other apps. – Simon Pham Mar 24 '20 at 08:42
  • 1
    Tested provided snapshot with Xcode 11.2.1 / macOS 10.15.3 - works fine. – Asperi Mar 24 '20 at 08:43
  • isn't TextEdit is just allowing text editing? Have you tried to drag the image to http://images.google.com/ search box ? – Simon Pham Mar 24 '20 at 09:01
  • 1
    In RichText mode (default) TextEdit allows including images. Search box in web accepts text & url, not image data. If to d&d image from TextEdit there nothing happens as well. – Asperi Mar 24 '20 at 09:10
  • oh thank you it's working. Do you know how to make it acceptable in Google Images search box? I'm just trying to make an app like Pasteapp.io that one can drag image to search box. I tried using Pasteapp.io to drag image to TextEdit but it doesn't work. Look like they don't use imagedata like us. – Simon Pham Mar 24 '20 at 09:16