iPhone:围绕图像包装文字

时间:2010-06-04 19:01:30

标签: iphone objective-c

根据我收集的内容,使用显示本地html文件的UIWebView可以在图像周围包装文本。 (我有一个名为index.html的本地html文件)

在viewDidLoad方法中插入的以下代码似乎会使应用程序崩溃并出错。 *** Terminating app due to uncaught exception 'NSInvalidArgumentException'

这个例子看起来很熟悉。这似乎是谷歌搜索中反复出现的例子。如果有人知道其他方式,示例或原因导致崩溃,请告诉我。

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] isDirectory:NO]]];

请注意,“webView”是UIWebView的IBOutlet,我能够将html字符串加载到其中;只是不是一个文件。此外,html文件的名称正好是“index.html”,如pathForResource和ofType中所述。

1 个答案:

答案 0 :(得分:1)

这就是我所做的,它有更多的错误处理和东西:

NSError *error = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" 
    ofType:@"html"];
html = [NSString stringWithContentsOfFile:filePath 
    encoding:NSUTF8StringEncoding error:&error];
if (error != nil)
    NSLog (@"Error loading index.html: %@", [error localizedDescription]);

NSString *basePath = [[NSBundle mainBundle] resourcePath];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
[webView loadHTMLString:html baseURL:baseURL];