You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

2 years ago
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2017-2021 Alexander Grebenyuk (github.com/kean).
  4. import Nuke
  5. import RxSwift
  6. #if !os(macOS)
  7. import UIKit
  8. #else
  9. import AppKit
  10. #endif
  11. extension ImagePipeline: ReactiveCompatible {}
  12. public extension Reactive where Base: ImagePipeline {
  13. /// Loads an image with a given url. Emits the value synchronously if the
  14. /// image was found in memory cache.
  15. func loadImage(with url: URL) -> Single<ImageResponse> {
  16. return self.loadImage(with: ImageRequest(url: url))
  17. }
  18. /// Loads an image with a given request. Emits the value synchronously if the
  19. /// image was found in memory cache.
  20. func loadImage(with request: ImageRequest) -> Single<ImageResponse> {
  21. return Single<ImageResponse>.create { single in
  22. if let image = self.base.cachedImage(for: request) {
  23. single(.success(ImageResponse(container: image))) // return synchronously
  24. return Disposables.create() // nop
  25. } else {
  26. let task = self.base.loadImage(with: request, completion: { result in
  27. switch result {
  28. case let .success(response):
  29. single(.success(response))
  30. case let .failure(error):
  31. single(.failure(error))
  32. }
  33. })
  34. return Disposables.create { task.cancel() }
  35. }
  36. }
  37. }
  38. }