在Html中嵌入图像并在UIWebView中加载

时间:2012-12-04 12:33:16

标签: iphone html ios cocoa-touch uiwebview

我知道如何在WebView中加载图像,现在我试图在html中嵌入图像并加载到UIWebView中。 以下是我正在使用的代码,

NSArray*    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*   documentsDirectoryPath = [paths objectAtIndex:0];
NSString*   folderPath =  [documentsDirectoryPath stringByAppendingPathComponent:@"iOSDevices"];
NSArray*    filesCount = [[NSFileManager defaultManager ] folderPath error:nil];
NSString* path = [NSString stringWithFormat:@"%@/%@",folderPath,[filesCount objectAtIndex:0]];

    NSString *htmlString = [NSString stringWithFormat:@"<html><body><img src=\"%@\" type=\"jpg\" width=\"320\" height=\"480\"></body></html>", path];
    [_webView loadHTMLString:htmlString baseURL:nil];

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

图片名称不可用,路径只获取图片路径,不在路径中提供图片,在标签中添加图片名称

答案 1 :(得分:1)

如果您尝试从本地加载图片,只需在src的前面添加file://,例如

NSString *htmlString = [NSString stringWithFormat:@"<html><body><img src=\"file://%@\" type=\"jpg\" width=\"320\" height=\"480\"></body></html>", path];
并检查您是否正在显示图片“jpg”或“png”,并检查您是否在NSString *htmlString = [NSString stringWithFormat:@"<html><body><img src=\"file://%@\" type=\"jpg\" width=\"320\" height=\"480\"></body></html>", path];标记中提供了正确的type

html字符串应该是, img

相关问题