图像无法在iphone中的UIWebView中加载

时间:2013-06-20 10:07:29

标签: iphone ios image uiwebview

我有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中来自服务器的fileName谢谢

3 个答案:

答案 0 :(得分:1)

我可以采用的另一种方法是动态地将html页面加载到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];

答案 1 :(得分:0)

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

NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;

答案 2 :(得分:0)

我认为你可以稍微收紧一下。不要获取字符串路径并从中创建文件URL,只需向bundle请求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]];