【问题标题】:UIWebView: Absolute path for images in app documents folderUIWebView:应用程序文档文件夹中图像的绝对路径
【发布时间】:2011-03-15 07:47:30
【问题描述】:

我有一个加载一些 html 的 UIWebView。它是一个 img 标签,我在 app 文档文件夹中保存了一张图片,我想在 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

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    哎呀,我太愚蠢了..只需要输入 src=\"file:/%@\"

    【讨论】:

    • 如果您不介意澄清一下:file:/%@ 返回什么?它应该返回 index.html 所在的相对路径吗?在我的情况下,您的建议返回:file:///%@
    猜你喜欢
    • 2012-09-27
    • 2013-12-20
    • 1970-01-01
    • 2015-02-17
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2017-04-02
    相关资源
    最近更新 更多