我正在通过NSURLProtocol向我的UIWebView提供本地图像(这意味着几乎立即返回图像),但我遇到的问题是缓存图像(图像在首次加载后再次显示)需要更长时间才能加载。我的NSURLProtocol中有没有引起这种情况的原因?
@implementation URLProtocol
+ (BOOL) canInitWithRequest:(NSURLRequest *)request {
return [request.URL.scheme isEqualToString:@"file"] ||
[request.URL.scheme isEqualToString:@"http"];
}
+ (NSURLRequest*) canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
- (void) startLoading {
id<NSURLProtocolClient> client = self.client;
NSURLRequest* request = self.request;
NSString *fileToLoad = request.URL.absoluteString;
NSURLResponse *response;
if([fileToLoad hasPrefix:@"http://app-fullpath/"]){
fileToLoad = [fileToLoad stringByReplacingOccurrencesOfString:@"http://app-fullpath/" withString:@""];
} else {
fileToLoad = [[NSURL URLWithString:fileToLoad] path];
}
NSData* data = [NSData dataWithContentsOfFile:fileToLoad];
response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:[NSDictionary dictionary]];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:data];
[client URLProtocolDidFinishLoading:self];
}
- (void) stopLoading { }
@end
任何速度建议,javascript / html或iOS?
答案 0 :(得分:1)
我的问题是UIWebView为文本提供了比图像更高的优先级,因此首先布置文本,然后处理图像。为了解决这个问题,我创建了HTML和HTML的DOM表示形式。图像,然后我用通过javascript(new Image()
)加载的图像替换所有图像,它们立即显示。