Read and Write the Data To and From Document Directory of IPhone

-(void)saveImagesInIPhone:(NSData*)imageData withName:(NSString*)imageName

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask, YES);  

 //Get the docs directory

    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

    NSString *folderPath = [documentsDirectoryPath  stringByAppendingPathComponent:@"IconImages"];  // subDirectory

     if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])

        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath  withIntermediateDirectories:NO attributes:nil error:nil];

    

    //Add the FileName to FilePath

    NSString *filePath = [folderPath stringByAppendingPathComponent:[iconName   stringByAppendingFormat:@"%@",@".png"]];

    //Write the file to documents directory

    [imageData writeToFile:filePath atomically:YES]; 

}

// O/P:- /var/mobile/Applications/20F869FD-5C61-4900-8CFE-830907731EC9/Documents/Designer0.png


-------------------------------------------------------------------------------------------------


-(UIImage*)retrieveImageFromPhone:(NSString*)fileNamewhichtoretrieve

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

     //Get the docs directory

    NSString *documentsPath = [paths objectAtIndex:0]; 


    NSString *folderPath = [documentsPath   stringByAppendingPathComponent:@"IconImages"];  // subDirectory


    NSString *filePath = [folderPath stringByAppendingPathComponent:

                          [fileNamewhichtoretrieve stringByAppendingFormat:

                            @"%@",@".png"]];


    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])

        return [[UIImage alloc] initWithContentsOfFile:filePath];

    else 

        return nil;

}


// O/P:- /var/mobile/Applications/20F869FD-5C61-4900-8CFE-830907731EC9/Documents/Designer0.png



Similar Articles