2011-09-09 4 views
0

Je fais une application dans laquelle j'utilise l'analyse JSON. Avec l'aide de json parsing je reçois l'URL de photo qui est enregistrée dans la chaîne. Pour afficher des images dans ma cellule j'utiliser ce codeComment enregistrer des images dans le répertoire personnel?

NSString *strURL=[NSString stringWithFormat:@"%@", [list_photo objectAtIndex:indexPath.row]]; 
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: strURL]]; 
CGRect myImage =CGRectMake(13,5,50,50); 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImage]; 
[imageView setImage:[UIImage imageWithData: imageData]]; 
[cell addSubview:imageView]; 

Maintenant prblem est que quand je vais revenir ou forword alors je l'ai attendre quelques secondes pour revenir sur la même vue. Maintenant, je veux que je lorsque l'application est utilisée d'abord puis j'attends cet écran sinon obtenir des images à partir du répertoire personnel. Comment puis-je enregistrer ces images dans mon répertoire personnel? Comment accéder à partir du répertoire de base?

+0

Voir si [ce] (http://stackoverflow.com/questions/2625702/caching-images-dans-iphone-app) aide – Saran

Répondre

0

Vous pouvez enregistrer une image dans le répertoire de documents par défaut comme suit en utilisant imageData;

// Accessing the documents directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.png"]; 

//Writing the image file 
    [imageData writeToFile:savedImagePath atomically:NO]; 
0

Vous pouvez l'utiliser pour écrire un fichier sur votre dossier Documents

+(BOOL) downloadFileFromURL:(NSString *) url withLocalName:(NSString*) localName 
{ 

    //Get the local file and it's size. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:localName]; 

    NSError *error; 
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:finalPath error:&error]; 
    NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error])); 
    if (error) return NO; 
    int localFileSize = [[fileAttributes objectForKey:NSFileSize] intValue]; 


    //Prepare a request for the desired resource. 
    NSMutableURLRequest *request = [NSMutableURLRequest 
            requestWithURL:[NSURL URLWithString:url]]; 
    [request setHTTPMethod:@"HEAD"]; 

    //Send the request for just the HTTP header. 
    NSURLResponse *response; 
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error])); 
    if (error) return NO; 

    //Check the response code 
    int status = 404; 
    if ([response respondsToSelector:@selector(statusCode)]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
     status = [httpResponse statusCode]; 
    } 
    if (status != 200) 
    { 
     //file not found 
     return NO; 
    } 
    else 
    { 
     //file found 
    } 

    //Get the expected file size of the downloaded file 
    int remoteFileSize = [response expectedContentLength]; 


    //If the file isn't already downloaded, download it. 
    if (localFileSize != remoteFileSize || (localFileSize == 0)) 
    {  
     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
     [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil]; 
     return YES; 
    } 
    //here we may wish to check the dates or the file contents to ensure they are the same file. 
    //The file is already downloaded 
    return YES; 
} 

et ceci à lire:

+(UIImage*) fileAtLocation:(NSString*) docLocation 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:docLocation]; 


    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 
    [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil]; 

    NSData *databuffer = [[NSFileManager defaultManager] contentsAtPath:finalPath];  
    UIImage *image = [UIImage imageWithData:databuffer]; 
    return image; 

} 
Questions connexes