UIWebView:应用程序文档文件夹中图像的绝对路径

4

我有一个加载某些html的UIWebView。其中包含一个img标签,我正在将图像保存在应用程序文档文件夹中,并希望在html中显示该图像。我是这样做的:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *tempImgFN = [NSString stringWithFormat:@"tempImg%d.jpg",storyRowID];
    NSString *tempImg = [documentsDir stringByAppendingPathComponent:tempImgFN];

    [imageBlob writeToFile:tempImg atomically:NO];
    tempImg = [tempImg stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    tempImg = [tempImg stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    [string appendString:[NSString stringWithFormat:@"<img style='max-width:120px' src=\"%@\" alt=\"\"/>",tempImg]];

imageBlob是带有图像数据的NSData。 图像已成功保存,其余HTML内容也能正常加载,但图片未显示。

如果将UIWebView的baseUrl设置为指向文档文件夹,则可以正常工作。 但我不想这样做,因为我希望baseURL是[NSBundle mainBundle] resourcePath]以访问一些JavaScript文件和CSS。

1个回答

2

糟糕,我太傻了...只需要将src=\"file:/%@\"放入即可。


1
如果您不介意能否再解释一下:file:/%@ 返回的是什么?它应该返回 index.html 所在的相对路径吗?在我的情况下,您的建议返回了:file:///%@ - sAguinaga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接