【问题标题】:Saving Images to document directory将图像保存到文档目录
【发布时间】:2011-01-09 13:44:35
【问题描述】:

我正在获取大约 1500 个图像 url,并想一次将这些图像存储在 iphone 的文档目录中? 请提出任何方法来做到这一点...

谢谢

【问题讨论】:

  • 我很少在这里发布我的问题....之前也有人建议过我...我知道这听起来很荒谬,但请告诉我如何接受?
  • 点击白色复选标记

标签: iphone image url


【解决方案1】:

下载所有URL并写入一个文件的方法:

     - (BOOL) downloadFilefromCDN :(NSString*)filename {

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/getlinks.php"]]];
        [request setHTTPMethod:@"GET"];

        NSError *err = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];
        if (err != nil) { return FALSE; } // Download error

        NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[searchPath objectAtIndex:0]  stringByAppendingString:[NSString stringWithFormat:@"/%@",filename]];
    if ([data writeToFile:path atomically:YES] != FALSE) {
        return TRUE;
    } else {
        return FALSE; // Error
    }

    }

您需要编写一个脚本来返回包含 1400 个链接的答案,以便将其写入该文件。

将 NSArray* 给出的所有图像下载到文档目录的方法。

- (id) downloadFilefromCDN :(NSString*)filename {

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/%@",filename]]];
    [request setHTTPMethod:@"GET"];

    NSError *err = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];
    if (err != nil) { return FALSE; }

    return data;

}

- (NSArray*) downloadFilesfromCDN :(NSArray*)filenames {

    NSMutableArray *mfiles = [[NSMutableArray alloc] init];

    BOOL error = FALSE;

    for (NSString* filename in filenames) {
        NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [[searchPath objectAtIndex:0]  stringByAppendingString:[NSString stringWithFormat:@"/%@",filename]];
            id file = [self downloadFilefromCDN:filename];
            if ([file isKindOfClass:[NSData class]]) {
        if ([(NSData*)file writeToFile:path atomically:YES] != FALSE) {
            [mfiles addObject:path];
        } else {
            error = TRUE; // Writing of file failed
        }
            } else {
                    error = TRUE; // Download failed
            }
    }

    if (error) { 
          // Handle error
    }

    [filenames release];

    return [NSArray arrayWithArray:mfiles];
}

注意:您需要将纯文件名读入 NSArray。如果你有不同的起源改变

[NSString stringWithFormat:@"http://example.com/%@",filename]

[NSString stringWithFormat:@"%@",filename]

并将完整路径(http://example.com/file1.png、http://example.com/file2.png)读入 NSArray。

完成。希望它会有所帮助。

抱歉编辑了这么多:S

【讨论】:

  • 它可以工作,但需要很多时间....可以简单地使用这个 UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"example.com/images/logo.png"]]] ;
  • 这取决于你想做什么。如果您想下载文件并获取它们的完整列表并将它们读入 UIImages,那么我的方法是完美的。如果您知道所有路径并且只想将它们读入 UIImages,那么您的路径是完美的。
猜你喜欢
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多