Correct way to load image into UIWebView from NSData object

前端 未结 10 2031
北恋
北恋 2020-12-03 05:26

I have downloaded a gif image into an NSData object (I\'ve checked the contents of the NSData object and it\'s definitely populated). Now I want to load that image into my U

相关标签:
10条回答
  • 2020-12-03 06:04

    try this code

    // 1) Get: Get string from “outline.plist” in the “DrillDownSave”-codesample.
    savedUrlString = [item objectForKey: @"itemUrl"];
    
    // 2) Set: The url in string-format, excluding the html-appendix.
    NSString *tempUrlString = savedUrlString;
    
    // 3) Set: Format a url-string correctly. The html-file is located locally.
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:tempUrlString ofType:@”html”];
    
    // 4) Set: Set an “NSData”-object of the url-sting.
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
    
    // 5. Gets the path to the main bundle root folder
    NSString *imagePath = [[NSBundle mainBundle] resourcePath];
    
    // 6. Need to be double-slashes to work correctly with UIWebView, so change all “/” to “//”
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    
    // 7. Also need to replace all spaces with “%20″
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    //  Load:   Loads the local html-page.
    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]];
    
    0 讨论(0)
  • 2020-12-03 06:09

    I did not really try to load image to UIWebView but a google search gives me. I think your image string must have a good path and looks like a URL

    NSString *imagePath = [[NSBundle mainBundle] resourcePath];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    NSString *HTMLData = @"
    <h1>Hello this is a test</h1>
    <img src="sample.jpg" alt="" width="100" height="100" />";
    [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];
    

    You can see more details here : Loading local files to UIWebView

    0 讨论(0)
  • NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"fireballscopy" ofType: @"gif"];
        NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
        [Web_View loadData:dataOfGif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    
    0 讨论(0)
  • 2020-12-03 06:10

    You may want to try assigning a delegate to the webview and implementing the method:

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    

    To see more specifically what error you're getting. If it doesn't get called, implement the method:

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    

    as well, just to make sure something is happening, otherwise there might be an issue with UIWebView (assuming you haven't returned NO from webView:shouldStartLoadWithRequest:navigationType:)

    0 讨论(0)
提交回复
热议问题