2013-07-16 28 views
5

Mam aplikację, w której mam folder o nazwie "Obrazy" w moim głównym pakiecie.Skopiuj folder z głównego pakietu do katalogu dokumentów w iphone

W folderze tym znajduje się inny folder o nazwie "Obrazy1" zawierający niektóre obrazy.

Po uruchomieniu aplikacji chcę mieć folder "Obrazy" w katalogu dokumentów.

Chcę pobrać obrazy z folderu "Images1".

Ale nie mogę dostać moje obrazy w folderze "Obrazy" w katalogu dokumentów z następującego kodu.

Edited

 BOOL success1; 
    NSFileManager *fileManager1 = [NSFileManager defaultManager]; 
    NSError *error1; 
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; 
    NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"Images/Images1"]; 
    success1 = [fileManager fileExistsAtPath:writableDBPath1]; 

    if (success1) return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images/Images1"]; 

    NSLog(@"Default : %@",defaultDBPath1); 
    success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1]; 

    if (!success1) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error1 localizedDescription]); 

    } 

Proszę mi pomóc.

+0

Na marginesie: przygotować się do app store odrzucenia, ponieważ najprawdopodobniej narusza [wytyczne przechowywania danych] (http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide /PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9). Nie należy zapisywać danych, które można łatwo odtworzyć w katalogu dokumentów. –

Odpowiedz

3

Czy jesteś pewien, że masz pliki w swoim drzewie folderów?

NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/Images/Images1"]; 

zwykle gdy xcode tworzy pakiet, spłaszcza zawartość katalogu. Najpierw sprawdź, czy masz wszystkie rzeczy w folderze źródłowym. Nawet jeśli zobaczysz, że folder jest wyświetlany w eksploratorze pakietów, po skopiowaniu w aplikacji zobaczysz, że obrazy znajdują się w folderze głównym. Aby tego uniknąć, po dodaniu folderu Obraz w projekcie xcode można zaznaczyć opcję "Utwórz odniesienie do folderu".

Jednak Twój zapis może się nie udać z powodu nadpisania. W takim przypadku musisz zaimplementować metodę delegowania dla NSFileManager podaną poniżej.

- (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:@"/Images"]; 
    success1 = [fileManager1 fileExistsAtPath:writableDBPath1]; 
    if (success1) 
    { 

     NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Images"]; 
     NSLog(@"default path %@",defaultDBPath1); 
     success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1]; 
    } else { 
     if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1]) 
      [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1]; 
    } 
} 

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

Tak, mam. Nie mogę zrozumieć, co jest nie tak w tym kodzie. Pomóż, jeśli wiesz. – Manthan

+0

Zaktualizowałem odpowiedź. – karim

+0

Myślę, że to jest klucz: createDirectoryAtPath :. Katalog nie istnieje w folderze dokumentów, a menedżer plików iOS nie utworzy automatycznie katalogów. Musisz je jawnie utworzyć. – Bek

Powiązane problemy