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
43 lines
1.4 KiB
// The MIT License (MIT)
|
|
//
|
|
// Copyright (c) 2017-2021 Alexander Grebenyuk (github.com/kean).
|
|
|
|
import Nuke
|
|
import RxSwift
|
|
|
|
#if !os(macOS)
|
|
import UIKit
|
|
#else
|
|
import AppKit
|
|
#endif
|
|
|
|
extension ImagePipeline: ReactiveCompatible {}
|
|
|
|
public extension Reactive where Base: ImagePipeline {
|
|
/// Loads an image with a given url. Emits the value synchronously if the
|
|
/// image was found in memory cache.
|
|
func loadImage(with url: URL) -> Single<ImageResponse> {
|
|
return self.loadImage(with: ImageRequest(url: url))
|
|
}
|
|
|
|
/// Loads an image with a given request. Emits the value synchronously if the
|
|
/// image was found in memory cache.
|
|
func loadImage(with request: ImageRequest) -> Single<ImageResponse> {
|
|
return Single<ImageResponse>.create { single in
|
|
if let image = self.base.cachedImage(for: request) {
|
|
single(.success(ImageResponse(container: image))) // return synchronously
|
|
return Disposables.create() // nop
|
|
} else {
|
|
let task = self.base.loadImage(with: request, completion: { result in
|
|
switch result {
|
|
case let .success(response):
|
|
single(.success(response))
|
|
case let .failure(error):
|
|
single(.failure(error))
|
|
}
|
|
})
|
|
return Disposables.create { task.cancel() }
|
|
}
|
|
}
|
|
}
|
|
}
|