0

I use SDWebImage to download the Pictures from my parsed XML file, and display it in a TableView. But the problem is that some images are not shown. This it how it looks like:

enter image description here

This is my code for parsing out the first image and displaying it in the TableView Cell:

// Parse out Image URL for cell
NSError *error = NULL;
NSRegularExpression *regexImage = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];

[regexImage enumerateMatchesInString:item.content
                        options:0
                          range:NSMakeRange(0, [item.content length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

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

                         [cell.imageView setImageWithURL:[NSURL URLWithString:src] placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
                     }];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178

1 Answers1

0

«Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.» source

I think your regex isnt prepared to handle german umlaute, as it is included in

http://www.floorballmagazin.de/wp-content/uploads/2013/03/Floorfighters-döbeln.jpg

it is hard (if not impossible) to write a regex, that handles html or just snippets of it correctly.

I wouldnt go down that road, if I was you. You either should parse it with a html parser, or — I'd prefer that in this case — use a NSScanner.


here a example of how NSScanner could be used:

 NSArray *imgtags = @[@"<p>sdf </p><img alt=\"\" src=\"http://www.floorballmagazin.de/wp-content/uploads/2013/03/Floorfighters-döbeln.jpg\" /> ",
                    @"<img src=\"http://www.floorballmagazin.de/wp-content/uploads/2012/01/20121501_nilsson_etv_as.jpg\" alt=\"Hat&#039;s nicht so mit blauem Dunst - Johan Nilsson.  / Foto: Andreas Schulz\" width=\"595\" height=\"384\" class=\"size-full wp-image-11542 colorbox-16600\" />"];    

[imgtags enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSScanner *scanner = [NSScanner scannerWithString:obj];
    [scanner scanUpToString:@"<img" intoString:NULL];
    [scanner scanUpToString:@"src" intoString:NULL];
    [scanner scanUpToString:@"=" intoString:NULL];
    [scanner scanUpToString:@"\"" intoString:NULL];
    [scanner setScanLocation:[scanner scanLocation]+1];
    NSString *s;
    [scanner scanUpToString:@"\"" intoString:&s];

    s = [s stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
    NSLog(@"%@", s);
}];

result:

http://www.floorballmagazin.de/wp-content/uploads/2013/03/Floorfighters-döbeln.jpg
http://www.floorballmagazin.de/wp-content/uploads/2012/01/20121501_nilsson_etv_as.jpg
Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178