Correct way to load image into UIWebView from NSData object

前端 未结 10 2067
北恋
北恋 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]]];
    

提交回复
热议问题