【问题标题】:Image does not load in UIWebView in iphone图像未在 iPhone 的 UIWebView 中加载
【发布时间】:2013-06-20 10:07:29
【问题描述】:

我有 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 中服务器的文件名中的图像,谢谢

【问题讨论】:

标签: iphone ios image uiwebview


【解决方案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];

【讨论】:

  • 所以你不认为它的图片 URL 不正确吗?因为你给我的 URL 在我的浏览器中不起作用
  • 好的..如果您只想显示图像,您可以使用 uiimageview 而不是 uiwebview...对于 AsyncImageView,我有更简单的解决方案,它不会将您的图像存储在本地。非常快速进行中
  • 请告诉我有没有办法检查网址是否正常
  • 只需复制您的网址并尝试粘贴到浏览器选项卡中...如果您可以在浏览器中看到该图像..那么它可以正常工作
  • 好的..现在我认为唯一的问题在于 urlstring ...别担心,只需转到此链接它会进一步帮助您stackoverflow.com/questions/1926117/…
【解决方案2】:

我认为你可以稍微收紧一点。无需获取字符串路径并从中创建文件 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]];

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多