我通過NSURLProtocol(這意味着圖像幾乎立即返回)提供本地圖像到我的UIWebView,但我遇到了緩存圖像(第一次加載後再次顯示圖像)的問題加載時間更長。我的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?
你爲什麼要加載本地圖像到UIWebView? – danypata
有許多用途,比如HTML應用程序,以及UIWebView比UITextView更容易操作的事實。這對我來說是最好的選擇。 – mattsven
另一個用例是加載本地化圖像。您可以請求本地png,在運行時檢查當前的語言環境並加載不同的映像。您也可以通過加載替代CSS來將其用於動態主題化,而無需更改HTML中的路徑。 –