0

我有 UIWebView 我想在其中加载图像但它没有显示图像我正在使用以下代码

     pdfView.hidden=NO;
    webView.hidden=NO;
    pdfView.backgroundColor=[UIColor clearColor];

    doneButton = [[UIBarButtonItem alloc]initWithTitle:@" Done " style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonClick)];            
    self.navigationItem.leftBarButtonItem =doneButton;


    webView.backgroundColor=[UIColor clearColor];


    NSLog(@"File Name is %@",fileName);

    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"];




    NSURL *url = [NSURL fileURLWithPath:fileName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];

    [webView loadRequest:request];      

有什么方法可以加载这个来自 webView 中服务器的文件名中的图像谢谢

4

3 回答 3

1

我可以采用的另一种方法是动态地 html-page 并将其加载到 UIWebView 中:

NSString *imageUrlString = @"your url ";
NSString *yourText = @"Some text here";

NSString *htmlString = [NSString stringWithFormat:@"<img src='%@'/><br><b>%@</b>", imageUrlString, yourText];    
[myWebView loadHTMLString:htmlString baseURL:nil];
于 2013-06-20T10:29:07.860 回答
0

我想你可以把它收紧一点。无需获取字符串路径并从中创建文件 URL,只需向捆绑包询问 URL。

我最好的猜测是您的文件名中已经包含文件扩展名,而您实际上并没有取回有效的文件路径/url。在调试器中单步执行并查看。

// get the file resource URL
NSURL *url = [[NSBundle mainBundle] URLForResource: filename withExtension: @"png"];

// sanity check - did we get a valid URL?
NSAssert( url != nil && [url checkResourceIsReachableAndReturnError: NULL], @"oops - no resource!");

// load it
[webView loadRequest: [NSURLRequest requestWithURL:url]];
于 2013-06-20T16:48:55.580 回答
-1

为什么不能只使用 UIImageView?这是手工完成的,但类似;

NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;
于 2013-06-20T10:16:46.723 回答