Correct way to load image into UIWebView from NSData object

前端 未结 10 2074
北恋
北恋 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 05:46

    Here's an alternative method:

    Save the image you downloaded into your documents folder. Then get that image's url. Then write a simple html file using that image url in the IMG SRC tag.

    NSLog(@"url=%@", fileURL); // fileURL is the image url in doc folder of your app
    
    
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/toOpen.html",
                          documentsDirectory];
    
    //create simple html file and format the url into the IMG SRC tag
    NSString *content = [NSString stringWithFormat:@"",fileURL];
    //save content to the documents directory
    [content writeToFile:fileName
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy
                   error:nil]; // now we have a HTML file in our doc
    
    // open the HTML file we wrote in the webview
    
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"life.html"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [yourWebView loadRequest:request];
    

提交回复
热议问题