我需要在我的 WebView 中从 iCloud 文档存储中加载图像我已经通过我自己的 NSURLProtocol 实现解决了这个问题。我的问题是,下载速度非常慢,用户在网页中看不到任何指示正在下载图像的指示符。
我想显示一个占位符图像,直到完成真实图像的下载:
public class EDAMResourceURLProtocol : NSURLProtocol
{
public override func startLoading() {
let url = self.request.URL!
let hash = url.host!
// I want to show this image before the real content gets downloaded
let image = UIImage(named: "DownloadPlaceholder")
// this function is very slow ....
historyStorage.fetchResourceWithBody(hash, success: {
edamResource in
let response = NSURLResponse(URL: self.request.URL!, MIMEType: edamResource!.mime, expectedContentLength: edamResource!.data!.body!.length, textEncodingName: nil)
self.client!.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: NSURLCacheStoragePolicy.NotAllowed)
self.client!.URLProtocol(self, didLoadData: edamResource!.data!.body)
self.client!.URLProtocolDidFinishLoading(self)
}
在真实内容可用之前,您有什么想法可以显示占位符吗?