【问题标题】:How to fetch HTML image from JSON in Objective C?如何在 Objective C 中从 JSON 中获取 HTML 图像?
【发布时间】:2017-12-15 00:38:06
【问题描述】:

这是我在带有表情符号的消息列中收到的 JSON:

<span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/smile.png\" height=\"20\" width=\"20\"></span> <span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/person_with_blond_hair.png\" height=\"20\" width=\"20\"></span>

【问题讨论】:

  • 看到这个链接可能对你有帮助stackoverflow.com/questions/12496860/…
  • Img Src会经常变化,我应该怎么给呢?
  • 在 HTML 标签中,图片 url 总是以 开头
  • @RomiPirisila 仅供参考,它不是JSON,它是纯 HTML。提取图片需要使用REGEX。
  • @Dipen Panchasara 它的纯 Html 我必须使用 JSON 解析到我的 IOS APP .....其中不知道如何将 html 图像 src 获取到 IOS APP

标签: ios


【解决方案1】:
NSString *htmslStr=@"<span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/smile.png\" height=\"20\" width=\"20\"></span> <span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/person_with_blond_hair.png\" height=\"20\" width=\"20\"></span>";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

[regex enumerateMatchesInString:htmslStr
                        options:0
                          range:NSMakeRange(0, [htmslStr length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                         NSString *img = [htmslStr substringWithRange:[result rangeAtIndex:2]];

                         NSURL *candidateURL = [NSURL URLWithString:img];

                         if (candidateURL && candidateURL.scheme && candidateURL.host)
                         {
                             NSLog(@"img src %@",img);
                         }


                     }];

最终的输出是

【讨论】:

  • 最后把这个img字符串用到你需要的地方
  • in htmslStr --> 传递你的儿子字符串
  • 谢谢@Anbu.Karthik
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2010-10-30
  • 2011-05-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
相关资源
最近更新 更多