I am trying to implement Drag and Drop into my application which shares images.
All my images are high performance thumbnails (i.e. small size) so I can't use them as my UIDragItem
, at least not as the final image.
What I am looking for is a way of providing the URL for my original image and send that off as the UIDragItem
and then have the destination fetch the image asynchronously. This is done in the Photos app when an image is stored in iCloud, so it must somehow be possible, I just cant seem to figure out how.
Turns out the solution is quite simple and is described in session 227 Data Delivery with Drag and Drop during this WWDC.
You basically make whatever object you want to drag conform to NSItemProviderWriting, and then implement two things.
NSItemProviderWriting:
Step one
Implement
writableTypeIdentifiersForItemProvider
which will give your receiver an idea of what type of object you are providing. This is an array of type identifiers with decreasing fidelity (they describe this well in the video)Step two
Implement
loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress?
which does the heavy lifting, this will get called as the receiver tries to load the object you are providing.Example
You can disregard the specifics of the data fetching below (I am using firebase) but using the native URLSession API would work the same way pretty much.
Then in our DragDelegate: