gpt4 book ai didi

ios - 将文件夹从主包复制到 iphone 中的文档目录

转载 作者:可可西里 更新时间:2023-11-01 04:44:16 30 4
gpt4 key购买 nike

我有一个应用程序,其中我的主包中有一个名为“Images”的文件夹。

在这个文件夹中有另一个名为“Images1”的文件夹,其中包含一些图像。

当我的应用程序启动时,我想要文档目录中的文件夹“Images”。

我想从文件夹“Images1”中获取图像。

但是我无法使用以下代码在文档目录的“图像”文件夹中获取我的图像。

已编辑

       BOOL success1;
NSFileManager *fileManager1 = [NSFileManager defaultManager];
NSError *error1;
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"Images/Images1"];
success1 = [fileManager fileExistsAtPath:writableDBPath1];

if (success1) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/Images1"];

NSLog(@"Default : %@",defaultDBPath1);
success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];

if (!success1) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error1 localizedDescription]);

}

请帮帮我。

最佳答案

您确定文件夹树中有这些文件吗?

NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/Images/Images1"];

通常当 xcode 制作一个包时,它会展平目录内容。因此,首先检查您的源文件夹中是否包含所有内容。即使您看到该文件夹​​显示在包资源管理器中,当在应用程序中复制时,您也会看到图像位于根文件夹中。为避免这种情况,当您在 xcode 项目中添加图像文件夹时,您可以选中“创建文件夹引用”选项。

但是,您的写入可能会因为覆盖而失败。在这种情况下,您必须为下面给出的 NSFileManager 实现委托(delegate)方法。

- (void) copyFolder {
BOOL success1;
NSFileManager *fileManager1 = [NSFileManager defaultManager];
fileManager1.delegate = self;
NSError *error1;
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"/Images"];
success1 = [fileManager1 fileExistsAtPath:writableDBPath1];
if (success1 )
{

NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"];
NSLog(@"default path %@",defaultDBPath1);
success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];
} else {
if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1])
[[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1];
}
}

- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
if ([error code] == 516) //error code for: The operation couldn’t be completed. File exists
return YES;
else
return NO;
}

关于ios - 将文件夹从主包复制到 iphone 中的文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669944/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com