ios - 无法将文件夹从应用程序包复制到文档目录

标签 ios iphone nsfilemanager

我正在尝试将一个文件夹从应用程序包复制到 iphone 文档目录,但它没有发生。

我用过这个代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];


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

    NSError *error1;
    NSArray *resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error: &error1];
    if (error1) {
        NSLog(@"error1 : %@",error1);
    }
    [resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         NSError* error;
         if (![[NSFileManager defaultManager]
               copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj]
               toPath:[documentsDirectory stringByAppendingPathComponent:obj]
               error:&error])
             NSLog(@"%@", [error localizedDescription]);
     }];
}

也试过这种方法

- (void)viewDidLoad{
     [self copyFolder];
    }

- (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:@"/Photos"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1])
        [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1];

    NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];
    NSLog(@"default path %@",defaultDBPath1);
    success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];
    if (!success1 )
    {
        NSLog(@"error1 : %@",error1);
    } else {
    }
}

- (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;
}

它显示这个错误

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x8a39800 {NSUnderlyingError=0x8a385b0 "The operation couldn’t be completed. No such file or directory", NSFilePath=/Users/user/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/app.app/Photos, NSUserStringVariant=( Folder )}

但是这是文件夹

enter image description here

最佳答案

Photos 看起来像是一组而不是文件夹引用。因此所有图像photo1.jpg .. 等直接存储在您的资源包下。

路径 <resource_bundle>/Photos不存在。要验证这一点,请转到应用程序 .app

.app->Right-click->Show Package Contents

不要将照片作为一个组添加,而是将其添加为文件夹引用,并在您的目标复制捆绑资源下确保添加整个文件夹。

希望对您有所帮助!

关于ios - 无法将文件夹从应用程序包复制到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013483/

相关文章:

ios - Objective-c - 如何将图像绘制为四边形?

iphone - 获取图像大小而不加载到内存

ios - 从 iCloud 驱动器获取文件而不下载?

php - 显示本地存储的图像以响应 JSON 表填充结果

android - 是否可以构建此 iPhone 应用程序? (电话使用/消费监控)

iphone - 如何在没有分隔符的情况下分割字符串?

iphone - 使用图像作为 backBarButtonItem,不带文本

ios - 如何访问应用程序目录外的文件

cocoa-touch - 使用 NSFileWrapper 写入与使用 NSFileManager 复制

ios - 没有自动换行的 UILabel