iphone - 从字符串中检索图像 url

标签 iphone objective-c ios

我正在解析一个 xml 文件,我可以 NSLog 解析,但我的问题是我需要从这个“字符串”中获取图像 url:

<p>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg"><img class="alignnone size-thumbnail wp-image-81" title="ex4" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg"><img class="alignnone size-thumbnail wp-image-80" title="ex3" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg"><img class="alignnone size-thumbnail wp-image-79" title="ex2" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg"><img class="alignnone size-thumbnail wp-image-71" title="ex1" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg" alt="" width="150" height="150" /></a>
 </p>

对不起,纯代码:)

我用来提取 url 的是这段代码,但它不起作用:

   NSRange start = [item.imageGallery       rangeOfString:@"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/"];
    NSRange end = [item.imageGallery rangeOfString:@"\" "];

    int rangeLength = (int)(end.location - start.location);

    NSString *hrefString = [[NSString alloc] initWithString:[item.imageGallery substringWithRange:NSMakeRange(start.location, rangeLength)]];
    NSLog(@"image url = %@",hrefString);

最佳答案

使用正则表达式:"src=\"([^\"]+)\""

下面是一些示例代码:

NSString *searchedString = @""  
    @"<p>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg\"><img class=\"alignnone size-thumbnail wp-image-81\" title=\"ex4\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg\"><img class=\"alignnone size-thumbnail wp-image-80\" title=\"ex3\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg\"><img class=\"alignnone size-thumbnail wp-image-79\" title=\"ex2\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg\"><img class=\"alignnone size-thumbnail wp-image-71\" title=\"ex1\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"</p>";
NSRange rangeOfString = NSMakeRange(0, [searchedString length]);
//NSLog(@"searchedString: %@", searchedString);

NSString *pattern = @"src=\"([^\"]+)\"";
NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matchs = [regex matchesInString:searchedString options:0 range:rangeOfString];
    for (NSTextCheckingResult* match in matchs) {
        NSLog(@"url: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
    }

NSLog 输出:

url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg

关于iphone - 从字符串中检索图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278195/

相关文章:

ios - 如何在应用程序 bundle 中编辑文件?

ios - AutoLayout 的按钮彼此重叠

iphone - 有没有iPhone sdk的TFTP客户端,可以传文件到TFTP服务器?

iphone - iOS 开发 : Can I store an array of integers in a Core Data object without creating a new table to represent the array?

iphone - 如何在 iOS 中创建多个值选择选择器?

ios - 将手势滑动到右上角、左上角等角落

ios - 存储数百张图像

iphone - 如何检查我的 UILabel 值是否大于 0?

javascript - 如何在plugin.xml中添加同名不同文件夹的资源?

ios - 如何确定我的 iOS 文档目录中是否存在某个名称的文件