简体   繁体   English

替换HTML字符串中的图像URL

[英]Replacing image URLs in html string

Hello I have a html string, with tags for every image. 您好,我有一个html字符串,每个图像都有标签。

I can extract the url of every image, but what I want is to save those images in the device, and then replace each one's image url with the corresponding path from my sandbox and then display them via UIWebView . 我可以提取每个图像的url,但是我想要的是将这些图像保存在设备中,然后用我的沙箱中的相应路径替换每个图像的url,然后通过UIWebView显示它们。

Any ideas how do I do that? 有什么想法我该怎么做?

Thank you! 谢谢!

Well if you have the URL for an image, you can save it like this 好吧,如果您有图像的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];

To replace the URLs in the HTML string simply use the method 要替换HTML字符串中的URL,只需使用方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM