美文网首页
iOS 取html字符串中的img

iOS 取html字符串中的img

作者: 吃瓜群众呀 | 来源:发表于2017-07-27 14:25 被阅读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;

    }

    相关文章

      网友评论

          本文标题:iOS 取html字符串中的img

          本文链接:https://www.haomeiwen.com/subject/bpuzkxtx.html