iPhone/iPad:无法将文件夹从 NSBundle 复制到 NSDocumentDirectory
我正在尝试复制 NSBundle 中的一个文件夹,其中包含大量图像。
我尝试用这些代码来做。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Images"];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Images"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
假设名为 Test.png 的文件夹中有一个图像,我想在按钮上显示该图像,但它不起作用!
但是,如果我只将一个图像从 NSBundle 复制到 NSDocumentDirectory,它就可以工作!
示例:
更改
stringByAppendingPathComponent:@"Images"
为
stringByAppendingPathComponent:@"Test.png"
所以问题在于将文件夹复制到 NSDocumentDirectory!
我上面的代码不正确吗?
或者无法复制文件夹? (这意味着我必须单独复制文件)
I am trying to copy a folder in my NSBundle which contains quite a number of images.
I tried doing it with these codes.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"Images"];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Images"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
Lets say there's an image in the folder named Test.png and i want to display the image on my button, it does not work!
However, if i only copied a single image from the NSBundle to NSDocumentDirectory, it works!
Example:
Changing
stringByAppendingPathComponent:@"Images"
To
stringByAppendingPathComponent:@"Test.png"
So the problem lies with copying the folder to NSDocumentDirectory!
Are my codes above incorrect?
Or it is impossible to copy folders? (Which means i have to copy files individually)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
奥斯卡是正确的,目前无法使用单个命令复制整个文件夹。
像这样的方法可能会成功(预警 - 我的办公室连接已关闭,我无法访问我的 Mac 来验证此代码是否正常工作。一旦可以,我将验证这一点)。只需使用 [self copyDirectory:@"Images"] 进行调用,它将处理其余的事情。
-- 编辑 2011 年 11 月 9 日 --
抱歉,正如我预先警告的那样,我无法访问我的 Mac 来验证代码。
更新了代码,我已经验证它现在可以正常工作。添加了几个快速 NSLogs 来告诉您新目录或新文件是否已经存在。
Oscar is correct, there is currently no way to copy an entire folder with a single command.
A method such as this might do the trick (forewarning - my office connection is down and I can't access my Mac to verify this code is working. Will verify this once I am able). Simply call with [self copyDirectory:@"Images"] and it will handle the rest.
-- EDIT 11/9/2011 --
Sorry about that, as I forewarned I was not able to access my mac to verify the code.
Updated the code and I have verified it is working properly now. Added a couple quick NSLogs to tell you if the new directory or the new file already exists.
恐怕目前还没有办法复制文件夹,如果有第三方功能可以做到这一点,那么它所做的只是逐个文件复制......因此您必须单独复制每个文件。
I am afraid there is currently no way to copy folders, and if there is a third party function that does it, all it will do really is copy file by file... Therefore you will have to copy each file individually.
下面的代码将在文档目录下创建一个名为“images”的文件夹,并将捆绑包中名为“folderinbundle”的文件夹中的所有文件复制到“images”
编辑澄清:如果“folderinbundle”是物理文件夹而不是物理文件夹,则上述代码将起作用团体。
The code below will create a folder called "images" under document directory and copy all files from a folder called "folderinbundle" in your bundle to "images"
Edit to clarify: The above will work if "folderinbundle" is a physical folder not a group.