Search code examples
iosobjective-cwebviewnsurl

iOS WebView, fixed image src name, create image (nsdata, nsbundle, nsurl) dynamically?


So my basic problem is, I have a webview containing simple code like

<img src="fixedname.jpg">

Now I need to set the baseurl so the webview can find the image, but my image is dynamically created, let's assume I have just the byte data in a NSData object, how can I add a filename to it or create a NSURL which I can provide the webview?

Regards


Solution

  • you may try to save image into local file and set the url of file instead of image name in web view. try the following
    NSData* data = UIImagePNGRepresentation(yourDynamicImage);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString* file= [documentDirectory stringByAppendingPathComponent:@"image.png"];
    [data writeToURL:[NSURL URLWithString:file] atomically:YES];
    NSURL url = [NSURL fileURLWithPath:file];
    

    // pass the [url absoluteString] to the web view this should work