2015-09-09 38 views
2

这是我收到的消息列通过表情符号在该JSON:如何从目标C中的JSON中获取HTML图像?

<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> 
+0

看到此链接可能是与你的帮助的http:// stackoverflow.com/questions/12496860/ios-strip-img-from-nsstring-a-html-string –

+0

Img Src会经常改变,所以我该如何给它? – Romi

+0

在HTML标记中,图片url总是像

回答

2
NSString *[email protected]"<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); 
         } 


        }]; 

最终走出看跌期权

enter image description here

+0

finally use this **img** string to where you need –

+0

in **htmslStr** -->开始传递你的儿子字符串 –

+0

谢谢@ Anbu.Karthik – Romi