Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.33 KB

RetrievingImageExample.md

File metadata and controls

49 lines (36 loc) · 1.33 KB

Retrieving an image from a web service example

The library comes with a concrete type, NetworkImageResource, which represents a simple image resource.

To retrieve an image, first create and NetworkImageResource with the relevant URL:

let imageURL = URL(string: "http://www.theappbusiness.com/apple-touch-icon-180x180.png")!
let imageResource = NetworkImageResource(url: imageURL)

Either

Use a NetworkDataResourceService directly:

let networkService = NetworkDataResourceService()
networkService.fetch(resource: imageResource) { (result) in
  if case let .success(image, _) = result {
    // Do something with the image. If using UIKit remember to dispatch to the main thread.
  }
}

OR

Use a ResourceOperation with GenericNetworkDataResourceService:

Optionally create a typealias of NetworkImageResourceOperation

typealias NetworkImageResourceOperation = ResourceOperation<GenericNetworkDataResourceService<NetworkImageResource>>
let operationQueue = OperationQueue()
let imageOperation = NetworkImageResourceOperation(resource: imageResource) { operation, result in
  if case let .success(image, _) = result {
    // Do something with the image. If using UIKit remember to dispatch to the main thread.
  }
}
operationQueue.addOperation(imageOperation)