解析字符串中所有的html中img标签的url

解析字符串中所有的html标签中的url连接 


#pragma mark 截取服务器传来的图片字符串

- (void)GetDescriptionAndImagesWithStringData:(NSString *)stringData {

    NSMutableArray *arrayDetailImages = [NSMutableArray array];

    // 解析菜品的描述

    NSRange range = [stringData rangeOfString:@"];

    _stringGreenDescri = [stringData substringToIndex:range.location];

    

    // 解析菜品的图片

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"]*/>" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];

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

    

    for (NSTextCheckingResult *item in result) {

        NSString *imgHtml = [stringData 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];

                

                MyPrint(@"正确解析出来的SRC为:%@", src);

                [arrayDetailImages addObject:src];

            }

        }

    }

    

    _arrayImages = arrayDetailImages;


}

你可能感兴趣的:(iOS软件开发)