2012-08-30 40 views
0

您好,我有一个html字符串,每个图片都带有标签。替换html字符串中的图片网址

我可以提取每个图像的url,但我想要的是将这些图像保存在设备中,然后将每个图像的url替换为我的沙箱中的相应路径,然后通过UIWebView显示它们。

任何想法我该怎么做?

谢谢!

回答

1

那么如果你有这个图片的URL,你可以将它保存这样

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:string]]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyFile"]; 
[imageData writeToFile:filePath atomically:YES]; 

要替换HTML字符串的URL只是使用的方法

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

+0

有更多高效的方法,为很多图像?我必须为每个图像做到这一点。除了有很酷的方式从webview中获取沙箱中的图像 - http://stackoverflow.com/questions/747407/using-html-and-local-images-within-uiwebview感谢您的回答! – DevFly

+0

我不认为有。您可以随时在后台执行此操作,并在需要时添加加载小部件或其他内容。至于沙盒的东西,如果你有,你按照你链接的文章中的描述进行操作,并使用我提到的方法用你的项目中的图像名称替换你的URL。 – Vlad

+0

哦,不要忘记为文件名添加一组额外的引号 – Vlad