1

我有例如:www.example.com/url。

例如,在该 URL 中,很少有目录,www.example.com/dir1/并且在单击时有图像www.example.com/dir1/image1.jpg

我的问题是我需要获取里面的所有文件www.example.com/dir1/,所以所有图片都在 web 上的那个目录中。基于这些名称,我可以获得最终的 url(如www.example.com/dir1/image1.jpg),但我需要获取图像的所有名称,但不知道如何获取。

谢谢。

4

2 回答 2

0

如果这是在您的服务器上,您需要一些机制来检索文件的名称。

例如,如果是 PHP,这是一个返回所有 JPG/PNG 文件的 JSON 响应的脚本:

<?php

header('Content-type: application/json');

$files = scandir('.');
$images = array();

foreach ($files as $file)
{
    switch(strtolower(substr(strrchr($file,'.'),1)))
    {
       case 'png':
       case 'jpeg':
       case 'jpg': $images[] = $file;
    }
}

echo json_encode($images);

?>

然后,您可以使用NSURLConnection(或 AFNetworking 或其他)来检索它并将 JSON 转换为NSArray.

例如,使用 AFNetworking:

NSURL *url = [NSURL URLWithString:@"http://yourwebserver.com/some/path/images.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([responseObject isKindOfClass:[NSArray class]])
        [self doSomethingWithImageNames:responseObject];
    else
        NSLog(@"expected array, received: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"AFHTTPRequestOperation error: %@", error);
}];
[op start];

或者NSURLConnection

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError) {
        NSLog(@"sendAsynchronousRequest error: %@", connectionError);
        return;
    }

    NSError *jsonError = nil;
    NSArray *imageNames = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

    if (jsonError) {
        NSLog(@"JSONObjectWithData error: %@", jsonError);
        return;
    }

    [self doSomethingWithImageNames:imageNames];
}];

如果您必须依赖 HTML 响应,虽然您通常不应该使用正则表达式,但在这个有限的用例中,您可能可以侥幸逃脱。就我而言,我的 Web 服务器使用<a href="...">filename</a>语法报告文件的链接,因此我可以使用以下内容获取这些href标签:

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    if (![responseObject isKindOfClass:[NSData class]]) {
        NSLog(@"Was expecting `NSData` and got %@", responseObject);
        return;
    }

    NSString *string = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a\\s[\\s\\S]*?href\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];

    NSMutableArray *results = [NSMutableArray array];

    [regex enumerateMatchesInString:string
                            options:0
                              range:NSMakeRange(0, [string length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                             [results addObject:[string substringWithRange:[result rangeAtIndex:1]]];
                         }];

    [self doSomethingWithImageNames:results];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"AFHTTPRequestOperation error: %@", error);
}];
[op start];
于 2013-10-16T16:43:02.800 回答
0

如果您看到 apache 目录列表,您可以解析该 html 并获取所有 .jpg 文件。

http://www.raywenderlich.com/14172/how-to-parse-html-on-ios是一个关于如何解析 HTML 的教程

于 2013-10-16T16:55:35.220 回答