在https://github.com/zalando/MapleBacon的文档之后,我创建了以下内容,它在Swift 2.3中工作:
import MapleBacon
fileprivate let
_manager: ImageManager
, _storage: DiskStorage
, _stopwatch: Stopwatch
, _urlsLoading: [ String ]
required init ()
{
_manager = ImageManager.sharedManager
_storage = DiskStorage( name: "MyStorageName" )
_stopwatch = Stopwatch()
_urlsLoading = []
}
func loadImageBy ( Url url: String )
{
guard let nsurl = URL( string: url ) else { return }
_urlsLoading.append( url )
_manager.downloadImage(
atUrl: nsurl
, cacheScaled: true
, imageView: nil
, storage: _storage
, completion:
{
[ unowned self ] ( image, error ) -> Void in
self._stopwatch.startWith(
Delay: 0.02
, ForListener: self._onStopwatchComplete
)
}
)
}
我发现使用我自己的Stopwatch
组件强制启用延迟0.02 MapleBacon.DiskStorage
以通过其image( forKey: )
方法提供图片:
fileprivate lazy var _onStopwatchComplete: ( String, Any? ) -> Void =
{
[ unowned self ] ( key: String, data: Any? ) -> Void in
var url: String, imageCount = 0
// loop through urls of images being loaded
for _ in 0..<self._urlsLoading.count
{
url = self._urlsLoading[ imageCount ]
// image found
if let img = self._storage.image( forKey: url )
{
print( img )
// remove from list of urls of images being loaded and...
self._urlsLoading.remove( at: imageCount )
}
// image not found: continue waiting
else
{
imageCount += 1
}
}
self._stopwatch.stop()
}
使用Swift 3.0版本的MapleBacon,即使延迟5秒也不会在_storage
中生成对图像的引用,就像Swift 2.3版本一样,{{1闭合要么。
使用completion: ( image, error ) ->
3.0.0
版本,需要做些什么才能成功地存储和检索来自MapleBacon
的图片?
谢谢[至少阅读]。
PS :我没有创建DiskManager
标记所需的分数。如果有人这样做,那就太棒了,谢谢你。
答案 0 :(得分:0)
MapleBacon的贡献者Jan Gorman认为这是MapleBacon 3.0.0中的一个真正的错误,并上传了一个修复程序(问题跟踪:https://github.com/zalando/MapleBacon/issues/38)。在Swift 2.3中运行的代码现在也适用于Swift 3.0。