Created
August 5, 2017 02:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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