0

我无法在 Swift 中成功设置 Siesta 的本地数据。我的目标是UIImageURL本地设置一个,这样就可以在没有下载时间的情况下显示这个本地图像。

为此,我将 URL 的图像数据设置为:

let resource = CustomRemoteImageView.imageCache.resource(myPhoto.url.absoluteString)
let imageData = UIImagePNGRepresentation(image)! // I've also tried putting the UIImage directly in there, because the transformation chain doesn't apply to local data, right?
let entity: Entity<Any> = Entity(content: imageData, contentType: "*/*") // I've played around with the content type too!
resource.overrideLocalData(with: entity)

然后,我使用了一个始终尝试将内容解析为图像的自定义服务:

private let imageTransformer =
    ResponseContentTransformer
        { Image(data: $0.content)}

convenience init() {
    self.init(standardTransformers: [])
    configure {
        $0.pipeline[PipelineStageKey.parsing].add(self.imageTransformer, contentTypes: ["*/*"])
    }
}

该系统适用于所有远程图像,但它似乎总是无法解析这个被覆盖的本地图像。似乎它正在尝试解析,但每次都失败。

即我得到Siesta.ResourceEvent一个

(Siesta.ResourceEvent) $R20 = newData {
  newData = network
}

但实际.typedContentnil

4

1 回答 1

2

overrideLocalData并且overrideLocalContent根本不与管道交互。Siesta 不会尝试解析您传递的内容;你覆盖的就是你的资源得到的。

此外,overrideLocalData不要overrideLocalContent失败。他们总是更新资源的内容。如果您调用这些方法,资源内容将与您传递的内容相匹配。

所以……问题不在于解析。可能是什么?

Entity.typedContent是应用于as?资源实体的快捷方式content。如果您得到 nil,则意味着 (1)content您传递给的实体的overrideLocalData是 nil 或 (2) 您调用的上下文类型与的实际运行时类型typedContent不匹配。content

如果您打印,您会看到什么resource.latestData.content?这将向您展示实际存在的内容,并将排除typedContent.

如果它不是 nil,则从网络请求中比较它的值并获取要匹配的类型。

如果它是 nil,那么要么是其他东西清除了内容,要么你首先传递了 nil 内容。试着SiestaLog.Category.enabled = .common看看你是否能发现它在哪里,或者没有设置正确的东西。

于 2018-10-13T22:48:20.807 回答