To setup an operation queue that downloads images asynchronously in Swift, follow these steps:
Create an OperationQueue
instance:
main.swift38 chars2 lines
Create a new Operation
subclass that will be responsible for downloading images. In the main
method of this subclass, fetch image data asynchronously and store it in a property:
main.swift459 chars20 lines
Create an instance of ImageOperation
for each image you want to download, passing in the URL of the image:
main.swift199 chars8 lines
Add all of the ImageOperation
instances to the operation queue:
main.swift67 chars2 lines
When the image downloads are complete, use the imageData
property of each ImageOperation
instance to create UIImage
objects:
main.swift175 chars6 lines
Note: If you need to display the images in a UI component, make sure to update the UI on the main thread.
gistlibby LogSnag