iOS 取html字符串中的img

2017-07-27  本文已影响16人  吃瓜群众呀

- (NSArray *)filterImage:(NSString *)html

{

NSMutableArray *resultArray = [NSMutableArray array];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(img|IMG)(.*?)(/>|>|>)" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];

NSArray *result = [regex matchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length)];

for (NSTextCheckingResult *item in result) {

NSString *imgHtml = [html substringWithRange:[item rangeAtIndex:0]];

NSArray *tmpArray = nil;

if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) {

tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];

} else if ([imgHtml rangeOfString:@"src="].location != NSNotFound) {

tmpArray = [imgHtml componentsSeparatedByString:@"src="];

}

if (tmpArray.count >= 2) {

NSString *src = tmpArray[1];

NSUInteger loc = [src rangeOfString:@"\""].location;

if (loc != NSNotFound) {

src = [src substringToIndex:loc];

[resultArray addObject:src];

}

}

}

return resultArray;

}

上一篇下一篇

猜你喜欢

热点阅读