延迟图像加载:[NSURL URLWithString:<imagepathfrommydocumentsdirectory>]在iPhone中返回nil </imagepathfrommydocumentsdirectory>

时间:2011-04-10 18:45:28

标签: iphone cocoa-touch uitableview ios4 lazy-loading

我的应用程序文档目录中有图像,但我想将tableView中的图像加载为Lazy Image Load。

所以我想用

[cell.imageView setImageWithURL:[NSURL URLWithString:<imagePathFromMyDocumentsDirectory>]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

现在,此[NSURL URLWithString:<imagePathFromMyDocumentsDirectory>"]返回 nil

应该做什么?

编辑:

NSString *FundNames = [[array1 objectAtIndex:j] valueForKey:@"FundName"];
        NSDate *dates=[NSDate date];
        NSDateFormatter *ab = [[NSDateFormatter alloc] init];
        //[ab setDateFormat:@"yyyy-MM-dd"];
        [ab setDateFormat:@"yyyy-MM-dd"];
        NSString *dateStr = [ab stringFromDate:dates];

    NSString *imageName =[NSString stringWithFormat:@"%@_%@.png",FundNames,dateStr]; 
    //NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDirectory = [self getImagePath];
    NSLog(@"Image Path : %@",documentsDirectory);
    NSError *error1;
    NSString *filepath1;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error1];
    if (files == nil) {
        NSLog(@"Error reading contents of documents directory: %@", [error1 localizedDescription]);
    }

    NSLog(@"FileName: %@",imageName);

    BOOL success = NO;

    for (NSString *file in files)
    {
        NSLog(@"file in Files is %@",file);
        if([file isEqualToString:imageName])
        {
            filepath1 = [documentsDirectory stringByAppendingPathComponent:file];
            NSLog(@"Full Path :%@",filepath1);
            success = YES;
            break;
        }
    }

NSURL *imageURL = [NSURL URLWithString:filepath1];
[cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"newfund.png"]];

此处, filepath1 等于<imagePathFromMyDocumentsDirectory>

4 个答案:

答案 0 :(得分:3)

尝试[NSURL fileURLWithPath:<imagePathFromMyDocumentsDirectory>"]而不是[NSURL URLWithString:<imagePathFromMyDocumentsDirectory>"]

答案 1 :(得分:0)

它返回nil,因为URL格式错误。如果您需要更多帮助,请发布您放入的确切字符串。

修改

这是如何创建文件网址的示例,请注意使用initFileURLWithPath:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appPath = [documentsDirectory stringByAppendingPathComponent:fileName];

NSURL *url = [[NSURL alloc] initFileURLWithPath:appPath];

答案 2 :(得分:0)

确保正确设置filePath。这是一种方法。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:@"newImage.jpg"];

    NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: myFilePath] autorelease];
    [cell.imageView setImageWithURL:fileURL
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

答案 3 :(得分:0)

我不确定这一点但是看到你的代码我发现filePath1是一个NSString,你要在其中分配文档目录中图像位置的路径。但字符串未在任何地方初始化,我的意思是您不需要使用alloc initWithString:stringWithString: