Romi
Romi

Reputation: 45

How to fetch HTML image from JSON in Objective C?

This is the JSON which I receive in message column with emoji:

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

Upvotes: 2

Views: 140

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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);
                         }


                     }];

the final Out put is

enter image description here

Upvotes: 2

Related Questions