2013-07-01 72 views
0

我在UIViewController中有一個UIWebView。我通過此功能,加載了UIWebView的內容:如何在webview上緩存圖像?

- (void)loadHTMLString:(NSString *)html { 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSURL *baseURL = [NSURL fileURLWithPath:path]; 
    [self.webView loadHTMLString:html baseURL:baseURL]; 
} 

如果我的內容包含了很多圖片,我創建通過FGallery(https://github.com/gdavis/FGallery-iPhone)畫廊。當我第一次打開UIViewController時,我在我的UIWebView中加載圖像和文本。我關閉一個應用程序並關閉網絡。然後我打開我的應用程序並打開一個UIViewController。我從緩存中加載我的UIWebView。但我有一個問題。如果我在關閉網絡之前打開了圖庫,則圖庫封面不會從緩存中加載,但圖庫中的所有圖像都會加載。如果我在關閉網絡之前沒有打開圖庫,則圖庫封面將從緩存中加載。畫廊的封面和畫廊的第一個圖像是相同的。

如果我開了一家畫廊,爲什麼圖庫的封面不能加載?

回答

1

可能是網頁視圖的合適答案

- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) 
    { 
     NSURL *URL = [request URL]; //Get the URL 
     if ([[URL scheme] isEqualToString:@"objc"]) { 
      webView = webViewRef; 
      SEL method = NSSelectorFromString([URL host]); 
      if ([self respondsToSelector:method]) 
      { 
       [self performSelector:method withObject:nil afterDelay:0.1f]; 
      } 
      return NO; 
     } 
    } 
    return YES; 
} 
-(void)takePicture 
{ 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    imagePicker.delegate = self; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    [[delegate viewController] presentModalViewController:imagePicker animated:YES]; 
    [imagePicker release]; 
} 

#pragma mark - Image Picker 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    NSData *flatImage = UIImageJPEGRepresentation(image, 0.1f); 
    NSString *image64 = [flatImage base64EncodedString]; 
    NSString *js = [NSString stringWithFormat: @"processImage('data:image/jpeg;base64,%@')", image64]; 
    [webView stringByEvaluatingJavaScriptFromString:js]; 
    [picker dismissModalViewControllerAnimated:YES]; 
} 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    //cancel was hit inside of the camera view 
    [picker dismissModalViewControllerAnimated:YES]; 
}