1

Webビューに入るWebソースの画像(つまり、バンドルソースではない)をキャッシュする必要があります。通常は、画像をリクエストしてbase-64に変換し、HTMLに貼り付けるだけですが、100枚の画像が並んでいる場合はそれができません。

そこで、少し調べて、サブクラス化NSURLCacheしてオーバーライドすることで画像リクエストを取得する方法を見つけましたcachedResponseForRequest。私のコードは以下の通りです。

リクエストは問題なく実行できます。データを取得して印刷したり、データからUIImageを作成してサイズを取得したりして、definitelyそこにあることを確認したりします。しかし、cachedResponseForRequest自分自身を返して見てみると、で、UIWebView欠落している画像が表示されます。私はこれについて1日苦しんでいて、それを理解することができません。私は例を次々と見てきましたが、葉巻はありません。

誰かが私がこれを解決するのを手伝ってもらえますか?それは私を狂わせています。

これが私のNSURLCacheサブクラスの.mファイルの内容です。GimageData受信した現在の画像データを保存するためのインスタンス変数です。

ご存知のとおり、http___をhttp://に検索/置換する理由は、http://リンクがこの方法で取得されないように見えるため、リンクを次のようなものに見せました。バンドルを取得したら、URLを元に戻して取得します。

- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
{
    NSURL *url = [request URL];

 NSLog(@"Url requested is: %@", [url absoluteString] );


 NSRange textRange;

 textRange = [[[url absoluteString] lowercaseString] rangeOfString:[@"http___" lowercaseString]];
 NSCachedURLResponse *cachedResponse = nil;

 if(textRange.location != NSNotFound) {

  //Does contain the substring
  NSLog(@"Found an http___ request");
  NSString *newURL = [[url absoluteString] substringFromIndex:textRange.location];
  newURL = [newURL stringByReplacingOccurrencesOfString:@"http___" withString:@"http://"];
  NSLog(@"New url: %@", newURL);

  TTURLDataResponse *response = [[TTURLDataResponse alloc] init];
  TTURLRequest *imageRequest = [[TTURLRequest alloc] initWithURL:newURL
                 delegate:self];
  imageRequest.response = response;
  [response release];
  [imageRequest sendSynchronously];


  if ( GimageData ) {
   NSLog(@"Response: %d as %@", [GimageData bytes], [GimageData class]);
   NSLog(@"Storing under: %@ ", request.URL );

   NSURLResponse* Nresponse = [[NSURLResponse alloc] initWithURL:request.URL 
                 MIMEType:nil 
             expectedContentLength:[GimageData length] 
               textEncodingName:nil];

   cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:Nresponse
                   data:GimageData];

   [imageRequest release];
   [Nresponse release];
   NSLog(@"Cached the response.");
  }

 }

 if (cachedResponse == nil) {
  NSLog(@"Response is nil so getting super's cache");
  cachedResponse = [super cachedResponseForRequest:request];
 }

    return cachedResponse;
}


- (void)requestDidFinishLoad:(TTURLRequest*)request {
 NSLog(@"Request did finish loading.");

 TTURLDataResponse *response = request.response;
// NSURL *url = [NSURL URLWithString:request.urlPath];

 GimageData = response.data;


 if ( GimageData ) {
  NSLog(@"And we got the data.");
 }
 else {
  NSLog(@"But there was no data in the response.");
 }

}
4

0 に答える 0