0

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

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

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

现在,当 ImagePicker 完成时,我使用以下内容保存文件(图像):

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

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

请帮帮我!

4

2 回答 2

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 给我我的文件。

于 2012-10-20T11:26:12.310 回答
0

这条线使问题

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];
于 2012-10-20T09:51:40.650 回答