繁体   English   中英

无法将目录从捆绑包复制到库

[英]Can't Copy a directory from Bundle to Library

我在这里有一个沮丧的问题,问题是我想将Bundle中的目录复制到我的应用程序中的Library路径中,但是代码无法做到这一点。 目录树显示在这里:

在此处输入图片说明

在复制目录之前,我想检查捆绑中的目录内容,但是数组返回0项。 我使用的代码是这样的:

NSString * templatesBundlePath = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:nil];
NSArray * packFolders = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL fileURLWithPath:templatesBundlePath] includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLIsDirectoryKey, nil] options:0 error:&error];

packfolders包含0个项目。

如果contain不为0,则尝试使用以下代码复制它:

NSString * templatesAppPath = NSApplicationSupportDirectoryPath(@"Templates");
[[NSFileManager defaultManager] copyItemAtPath:templatesBundlePath toPath:templatesAppPath error:&error];

当我尝试使用此代码检查分发包目录大小时

NSFileManager *fm = [[NSFileManager alloc] init];

resultSize = [[[fm attributesOfItemAtPath:[[NSURL fileURLWithPath:templatesBundlePath]  path] error:nil] objectForKey:NSFileSize] unsignedIntegerValue];

NSLog(@"Size: %lu", (unsigned long)resultSize);

该值仅显示68,但finder中的实际大小为200MB

有没有像我这样的问题的人? 有人可以帮我吗?

谢谢

项目文件夹为黄色,这意味着它们是组而不是文件夹。 在将资源与应用程序捆绑在一起时,目录结构将被删除,所有文件都放在捆绑包的根目录中。

只需从项目中删除所有组,然后将文件夹拖回到项目中即可,现在选择创建文件夹而不是组。

在此处输入图片说明

请尝试此操作以获得black.jpg图像路径:

NSString * strpath = [NSString stringWithFormat:@“%@”,[[NSBundle mainBundle] pathForResource:@“ Templates” ofType:@“”]]; strpath = [strpath stringByAppendingString:@“ /”]; strpath = [strpath stringByAppendingString:@“ 80s”]; strpath = [strpath stringByAppendingString:@“ /”]; strpath = [strpath stringByAppendingString:@“ 80s_a-team”]; strpath = [strpath stringByAppendingString:@“ /”]; strpath = [strpath stringByAppendingString:@“ black”]; strpath = [strpath stringByAppendingString:@“。jpg”];

尝试遵循以下代码

  NSArray *libraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *directoryPath = [libraryPath objectAtIndex:0];
         NSString *imagePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Templete"];

                // Copy the image Folder from the package to the users filesystem
                [fileManager copyItemAtPath:imagePathFromApp toPath:directoryPath error:nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM