Unable to get images from a link

68 Views Asked by At

I am trying to get images from a linked page in iOS but it is not working.

In this link, I have the below image tag

<img src="/newsimg/newsmainimg/1433410900_andhra-pori.jpg" 
     alt="High Court Dismisses 'Andhra Pori's Case" 
     class="img-responsive center-block visible-xs">

and I am trying to get it using the below code

NSString *gossipsXpathQueryString = @"//td//img";W

What can I change in this string @"//td//img" to get the image correctly.

1

There are 1 best solutions below

1
On BEST ANSWER

Please check below code -

 NSURL *url = [NSURL URLWithString:@"http://www.cinejosh.com/news/3/40889/high-court-dismisses-andhra-poris-case.html"];
    NSString *webData= [NSString stringWithContentsOfURL:url];
   // NSLog(@"%@",webData);


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

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

                             NSString *img = [webData substringWithRange:[result rangeAtIndex:2]];
                             NSLog(@"img src %@",img);
                         }];

You will be get all img src from above code .