2014-04-21 5 views
0

Télécharger des fichiers image ou mp3 à partir du Web vers le répertoire iPhone de documentsTélécharger des fichiers image ou mp3 à partir du Web vers le répertoire iPhone de documents

Salut tout le monde

J'utilise ce code pour télécharger des fichiers de mon Web Site

Tout d'abord, je vérifie si le fichier existe

- (void)checkFile 
{ 

    image_path = @"background2.jpg"; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *libraryDirectory = [paths objectAtIndex:0]; 

    NSString *myFilePath = [libraryDirectory stringByAppendingPathComponent:image_path]; 

    BOOL fileExists = [fileManager fileExistsAtPath:myFilePath]; 

    if (fileExists) { 
     NSLog(@"file exist"); 
     self.myImage.image = [UIImage imageWithContentsOfFile:myFilePath]; 

    } else { 
     NSLog(@"file not exist"); 

     [self downloadImageFile:@"http://www.alhikmeh.org/images/background2.jpg" newFileName:image_path]; 



} 

Ce code pour le téléchargement:

- (void)downloadImageFile:(NSString *)path newFileName:(NSString *)newFileName 
{ 
    NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
    NSData *data = [NSData dataWithContentsOfURL:URL]; 

    if (data == nil) { 
     NSLog(@"error"); 

    } 
    NSString *appDocDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *storePath = [appDocDir stringByAppendingPathComponent:newFileName]; 
    BOOL success = [data writeToFile:storePath atomically:YES]; 

    if (success) { 
     NSLog(@"success"); 
    } else { 
     NSLog(@"not copied"); 
    } 


} 

L'impression du journal « succès » mais pas copier le fichier dans le document Directory !!

+0

façon simple et facile à cette utilisation AFNetworking https : //github.com/AFNetworking/AFNetworking –

Répondre

2

Let s essayer: (voir mon commentaire)

- (void)downloadImageFile:(NSString *)path newFileName:(NSString *)newFileName 
{ 
NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
NSData *data = [NSData dataWithContentsOfURL:URL]; 

if (data == nil) { 
    NSLog(@"error"); 

} 
NSString *appDocDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString *storePath = [appDocDir stringByAppendingPathComponent:newFileName]; 
// My comment : make sure that you check whether this file exists at above path??? 
// I run this code, and it can save data. 
NSlog(@"%@",storePath); 
BOOL success = [data writeToFile:storePath atomically:YES]; 

if (success) { 
    NSLog(@"success"); 
} else { 
    NSLog(@"not copied"); 
} 
} 
+0

c'est ma faute, charger le fichier de 'NSLibraryDirectory' et enregistrer dans' NSDocumentDirectory', merci –

+0

Puis-je faire une barre de progression pour ce téléchargement? –

+0

Bien sûr, vous devriez télécharger dans le fil de fond. Pendant le téléchargement, vous calculez la taille à afficher dans la barre de progression. http://stackoverflow.com/questions/16453655/objective-c-downloading-file-with-progress-bar – nmh

1

Je me suis sucessed en utilisant votre code, vérifiez votre document directory.The chemin est

/Users/userName/Library/Application Support/iPhone Simulator/7.1/Applications/your project/Documents/background2.jpg 
Questions connexes