Reputation: 3517
func showImageForUrl(url: String) -> Observable<Async<Any>> {
let result = RxAlamofire
.requestJSON(.get,
url,
parameters: nil)
.flatMap { (response, json) -> Observable<Any> in
return Observable.just(json)
}.async()
return result
}
url String "http://1.bp.blogspot.com/-KhiJ6WYQn8Q/T7ZXxb_KHxI/AAAAAAAABvM/_l134PCuEcA/s1600/dog+photos+3.jpg"
I'm trying this but the end result is an error.
Upvotes: 1
Views: 893
Reputation: 1434
I think one of the issue is the format of your request: it should be requestData(...)
instead requestJSON(...)
to download data then convert it into image.
Here is an example a bit simpler than the previous code
func showImageForUrl(url: String) -> Observable<UIImage?> {
return RxAlamofire
.requestData(.get, url)
.map({ (response,data) -> UIImage? in
return UIImage(data: data)
})
}
You can use it as following
showImageForUrl(myUrl)
.bind(to: myImageView.rx.image)
.disposed(by: disposeBag)
You can go even further by filter out when invalid response, or when UIImage
is nil, etc. Hope it helps.
Upvotes: 2