Skip to content

Instantly share code, notes, and snippets.

@smileyborg
Created August 5, 2017 02:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smileyborg/169104dfb55c7c86e73422007bf8894e to your computer and use it in GitHub Desktop.
Save smileyborg/169104dfb55c7c86e73422007bf8894e to your computer and use it in GitHub Desktop.
Snippet showing how to create an NSItemProvider with a simulated delay for loading an image
import UIKit
import MobileCoreServices
let image = UIImage() // your actual image
let itemProvider = NSItemProvider()
itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG as String, visibility: .all) { (completionBlock) -> Progress? in
let unitsOfWork = 10 + Int64(arc4random_uniform(UInt32(10))) // 10 - 19 units
let progress = Progress.discreteProgress(totalUnitCount: unitsOfWork)
DispatchQueue.global(qos: .userInitiated).async {
for _ in 0...unitsOfWork {
// Delay (sleep) 0.2 second for each unit of work
let delay = UInt32(0.2 * Double(USEC_PER_SEC))
usleep(delay)
progress.completedUnitCount += 1
}
completionBlock(UIImageJPEGRepresentation(image, 0.8), nil)
}
return progress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment