iOS 在 UIWebView 中加载图像

标签 ios image cocoa-touch uiwebview

拼命想弄清楚如何通过 UIWebView 将本地镜像(保存在本地 xcode 包中)加载到以下 HTML 中。我花了无数个小时遵循其他指南,但似乎没有任何效果,全都抛出错误。我正在尝试在以下 NSString 中添加本地镜像,其中我已编写 IMAGEGOESHERE -

{
    [super viewDidLoad];

    self.title = _restaurant.title;

    [[self navigationController] setNavigationBarHidden:NO animated:NO];

    self.activityIndicatorView.hidden = NO;
    [self.activityIndicatorView startAnimating];

    [self loadImageInNewThread];


    NSString *webViewContent = [NSString stringWithFormat:@"<html><head><style>* {font-family:   Helvetica}</style></head><body><center>%@ - %@<br><br><b>Restaurant:</b>%@</b><br>IMAGEGOESHERE<br></center></b></body></html>", _restaurant.openingTime,_restaurant.closingTime, _restaurant.name];

    [self.webView loadHTMLString:webViewContent baseURL:nil];
}

我希望你能帮忙,因为它让我发疯!

最佳答案

您需要首先引用图像的路径:

 NSString *pathImg = [[NSBundle mainBundle] pathForResource:@"yourimage" ofType:@"png"];

然后在 webViewContent 中指定您的路径以及图像的大小:

NSString* webViewContent = [NSString stringWithFormat:
                                   @"<html>"
                                   "<body>"
                                   "<img src=\"file://%@\" width=\"200\" height=\"500\"/>"
                                   "</body></html>", pathImg];

关于iOS 在 UIWebView 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093697/

相关文章:

ios - Swift Static TableView 添加额外的行

objective-c - 在Objective-C中存储和加载大量数据的最佳方法

ios - 使用 Cocoapods v1.0.x 拉取 pod 时未构建 MagicalRecord

objective-c - 如何在 iOS/Objective C 中获取 TouchPoint 的坐标和像素颜色

ios - 为 uipopoverview 使用导航 Controller 的最佳方式

javascript - 如何确定图像是否在 Javascript 上使用透明度?

ios - 如何使用CGAffineTransform缩放比例并同时翻转

带有 TouchableOpacity onpress 的图像在 React Native 中不起作用

c++ - 在OpenCV C++中拼接2张图像后去除图像的黑色部分

objective-c - UISplitViewController 中缺少 UIBarButtonItem