簡體   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