gpt4 book ai didi

ios - 如何使用ReactiveKit将本地镜像绑定(bind)到 ImageView ?

转载 作者:行者123 更新时间:2023-11-30 13:33:03 25 4
gpt4 key购买 nike

为了从服务器获取图像,我已经这样做了

// 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()
}
}

}

然后将其绑定(bind)到 ImageView

if (url != nil)
{
let image : ObservableBuffer<UIImage>? = self.fetchImage(url!).shareNext()

if ((image) != nil)
{

image!.bindTo(customCellObj.mContentCellImageView)
}
}

这里我使用 almofire 从 url 获取图像。有什么方法可以直接使用 react 套件将本地数据库图像与 ImageView 绑定(bind)?

最佳答案

以下是将 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)

还有一个 ReactiveKit 的 Alamofire 扩展 (AlamofireReactive),您可能会发现它很有用。

关于ios - 如何使用ReactiveKit将本地镜像绑定(bind)到 ImageView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com