Drag and Drop asynchronous data fetching

Viewed 1400

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.

2 Answers

This code for Drag a PHAsset

extension PHAsset : NSItemProviderWriting {
    public static var writableTypeIdentifiersForItemProvider: [String] {
        return UIImage.writableTypeIdentifiersForItemProvider
    }
    public func loadData(withTypeIdentifier typeIdentifier: String,
                         forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
        PHImageManager.default().requestImageData(for: self, options: nil) { (data, _, _, _) in
            completionHandler(data, nil)
        }
        return nil
    }
}

Use:

let item = UIDragItem(itemProvider: NSItemProvider.init(object: yourasset))
Related