从服务器获取图像我已经完成了这个
// fetch image from server
func fetchImage(url: NSURL) -> Operation<UIImage, NSError> {
return Operation { observer in
// use almofire to deal with server request
let request = Alamofire.request(.GET, url).response { request, response, data, error in
// if error occurs then abort the operation
if let error = error {
observer.failure(error)
} else {
// if doesnt occurs error then convert imageData back to image
if(data != nil)
{
observer.next(UIImage(data : data!)!)
observer.success()
}
}
}
// if response is nil then execute this block
return BlockDisposable {
request.cancel()
}
}
}
&安培;然后将其绑定到图像视图
if (url != nil)
{
let image : ObservableBuffer<UIImage>? = self.fetchImage(url!).shareNext()
if ((image) != nil)
{
image!.bindTo(customCellObj.mContentCellImageView)
}
}
这里我使用almofire从url获取图像。有没有办法可以直接使用反应套件将本地DB图像与图像视图绑定?
答案 0 :(得分:0)
以下是将imageView连接到流的方法:
Stream.just(NSURL(string: "https://exoticcars.enterprise.com/etc/designs/exotics/clientlibs/dist/img/homepage/Homepage-Hero-Car.png")!)
.flatMapLatest(fetchImage)
.flatMapError { _ in return Stream.just(UIImage()) }
.observeNext { [weak self] image in self?.imageView.image = image }
.disposeIn(bag)
然而,听起来有太明显的风险......这就是ReactiveUIKit的用途。
Stream.just(NSURL(string: "https://exoticcars.enterprise.com/etc/designs/exotics/clientlibs/dist/img/homepage/Homepage-Hero-Car.png")!)
.flatMapLatest(fetchImage)
.flatMapError { _ in return Stream.just(UIImage()) }
.bindTo(imageView.rImage)
.disposeIn(bag)
还有一个Alamofire扩展到ReactiveKit(AlamofireReactive)你可能会觉得有用。