【问题标题】:iOS WebView, fixed image src name, create image (nsdata, nsbundle, nsurl) dynamically?iOS WebView,固定图像src名称,动态创建图像(nsdata,nsbundle,nsurl)?
【发布时间】:2014-04-30 06:37:41
【问题描述】:

所以我的基本问题是,我有一个包含简单代码的 webview,例如

<img src="fixedname.jpg">

现在我需要设置 baseurl 以便 webview 可以找到图像,但是我的图像 是动态创建的,假设我只有 NSData 对象中的字节数据, 如何向其中添加文件名或创建可以提供 webview 的 NSURL?

问候

【问题讨论】:

    标签: ios objective-c webview nsurl


    【解决方案1】:

    您可以尝试将图像保存到本地文件中,并在 Web 视图中设置文件的 url 而不是图像名称。 试试下面的
    NSData* 数据 = 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];
    

    // 将 [url absoluteString] 传递给 web 视图 这应该工作

    【讨论】:

    • 谢谢工作正常!我希望以某种方式通过管道而不是复制文件,但效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2011-12-07
    • 2011-01-29
    • 2010-09-26
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多