【问题标题】:After Update, App Doesn't Load Existing Files from Documents Folder更新后,应用程序不会从 Documents 文件夹加载现有文件
【发布时间】:2012-10-20 09:45:46
【问题描述】:

我在临时更新后从应用的文档文件夹加载现有图像时遇到问题。

我在互联网上搜索了答案,我发现我必须使用文件的相对路径,以便在更新应用程序时路径保持不变。

有人可以告诉我怎么做吗?

现在我使用以下方法在 ImagePicker 完成时保存文件(图像):

NSString *imageFilename = [NSString stringWithFormat:@"%@-profilePhoto.jpg",[NSDate date]];
NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

imagePath = [NSString stringWithFormat:@"%@/%@", paths, imageFilename];

请帮帮我!

【问题讨论】:

  • 请 NSLog 图像路径和控制台上显示的内容?

标签: objective-c ios xcode nsfilemanager


【解决方案1】:

我找到了解决办法:

我没有从完整路径加载图像,而是选择让系统在它们的名称之后搜索它们。

所以而不是:

//Full path stored in a dictionary:
    profilePhotoPath = [userDict objectForKey:@"profilepic"];
//Load the image from path:
    profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];

我现在使用以下内容:

//Load image from documentsDirectory, filename
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    profilePhotoPath = [userDict objectForKey:@"profilepic"];
    profilePhotoPath = [documentsDirectory stringByAppendingPathComponent:[profilePhotoPath lastPathComponent]];
    profilePhoto = [[UIImage alloc] initWithContentsOfFile:profilePhotoPath];

通过使用“lastPathComponent”属性,我基本上从路径中删除了除文件名之外的所有内容,然后我使用 NSSearch 给我我的文件。

【讨论】:

    【解决方案2】:

    这一行出了问题

    NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    

    这里的问题是变量路径的类型是NSArray 而不是NSString

    改成,

    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *paths = [path objectAtIndex:0];
    

    【讨论】:

    • 我不起作用,因为我之前尝试过以下操作:imagePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], imageFilename];
    • 好的,我现在去看看。感谢您尝试帮助我!
    • 很高兴,希望这能帮助您解决问题:)
    • 不幸的是它不起作用...只是再次测试它,更新后它不会加载图像文件,因为目录的路径已更改。这就是为什么我需要图像的相对路径
    • 图像路径:/var/mobile/Applications/70BEA46A-1C8B-4415-B6B3-6D6204316CF1/Documents/2012-10-20 10:10:54 +0000-profilePhoto.jpg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多