Ios 将应用程序中的图像复制到文档目录,然后重写它们

Ios 将应用程序中的图像复制到文档目录,然后重写它们,ios,uiimage,nsbundle,nsdocumentdirectory,Ios,Uiimage,Nsbundle,Nsdocumentdirectory,我有一个应用程序,其中有一定数量的.jpg图片(大约300张)。它们可以作为一个开始,因为它们实际上位于互联网上,但显然,用户不必在应用程序的第一个开始就下载所有这些文件,而是将它们预先打包,这样会更方便 每次我从服务器获得新信息时,我都需要重写这些图像。显然,我无法触摸应用程序包,因此我看到了如下步骤: 在应用程序的第一次启动时,将图像从捆绑包解压缩到Documents目录 只能从Documents目录访问它们,而不能从bundle访问 如果有必要,我应该重写它们 因此,我的代码将是统一的,因

我有一个应用程序,其中有一定数量的.jpg图片(大约300张)。它们可以作为一个开始,因为它们实际上位于互联网上,但显然,用户不必在应用程序的第一个开始就下载所有这些文件,而是将它们预先打包,这样会更方便

每次我从服务器获得新信息时,我都需要重写这些图像。显然,我无法触摸应用程序包,因此我看到了如下步骤:

  • 在应用程序的第一次启动时,将图像从捆绑包解压缩到Documents目录
  • 只能从Documents目录访问它们,而不能从bundle访问
  • 如果有必要,我应该重写它们
  • 因此,我的代码将是统一的,因为我将始终使用相同的路径来获取图像

    问题是,我对iOS中的整个文件系统知之甚少,因此我不知道如何将特定的包内容解压缩到Documents目录,也不知道如何写入Documents目录

    你能帮我写一些代码并确认我的解决方案是正确的吗

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"images"];  //optionally create a subdirectory
    
    //"source" is a physical folder in your app bundle.  Once that has a blue color folder (not the yellow group folder)
    // To create a physical folder in your app bundle: drag a folder from Mac's Finder to the Xcode project, when prompts
    // for "Choose options for adding these files" make certain that "Create folder references for …" is selected.
    // Store all your 300 or so images into this physical folder.
    
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"source"];  
    NSError *error;
    [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&error];
    if (error)
        NSLog(@"copying error: %@", error);
    
    根据OP中的附加注释进行编辑:

    要使用相同的文件名重写到相同的目录,可以在写入之前使用fileExistsAtPath和removeItemAtPath的组合来检测并删除现有文件

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    }
    // now proceed to write-rewrite
    
    试试这个代码

    -(void)demoImages
    {
    
    //-- Main bundle directory
    NSString *mainBundle = [[NSBundle mainBundle] resourcePath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = [[NSError alloc] init];
    NSArray *mainBundleDirectory = [fm  contentsOfDirectoryAtPath:mainBundle error:&error];
    
    NSMutableArray *images = [[NSMutableArray alloc]init];
    for (NSString *pngFiles in mainBundleDirectory)
    {
        if ([pngFiles hasSuffix:@".png"])
        {
            [images addObject:pngFiles];
        }
    }
    
    NSLog(@"\n\n Doc images %@",images);
    //-- Document directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //-- Copy files form main bundle to document directory
    for (int i=0; i<[images count]; i++)
    {
        NSString *toPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[images objectAtIndex:i]];
        [fileManager copyItemAtPath:[NSString stringWithFormat:@"%@/%@",mainBundle,[images objectAtIndex:i]] toPath:toPath error:NULL];
        NSLog(@"\n Saved %@",fileManager);
    }
    
    
    }
    
    -(无效)演示图像
    {
    //--主捆绑目录
    NSString*mainBundle=[[NSBundle mainBundle]resourcePath];
    NSFileManager*fm=[NSFileManager defaultManager];
    NSError*error=[[NSError alloc]init];
    NSArray*mainBundleDirectory=[fm内容目录路径:mainBundle错误:&error];
    NSMutableArray*图像=[[NSMutableArray alloc]init];
    用于(主BundleDirectory中的NSString*pngFiles)
    {
    如果([pngFiles hasSuffix:@.png”])
    {
    [图像添加对象:pngFiles];
    }
    }
    NSLog(@“\n\n文档图像%@”,图像);
    //--文件目录
    NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
    NSString*documentDirectory=[paths objectAtIndex:0];
    NSFileManager*fileManager=[NSFileManager defaultManager];
    //--将文件从主捆绑包复制到文档目录
    
    对于(int i=0;iThanks,但是在该目录下重写这些文件怎么样?@morgan1189只需像在diskuser523234中写入普通文件一样重写它们。解释非常清楚,而且有效。谢谢。