1

I have a tableView where for each cell I'm getting an image url string and I'm saving all data in the NSDocument directory.

I want to store all the data in folder made in the NSDocument directory and I also want to delete all the contents in that folder later. How to do it?

this is the method I'm using

(void)setThumbnailTo :(UITableViewCell *)cell withRefString:(NSString *)string   {


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

   NSString *documentsDirectory = [paths objectAtIndex:0]; 


   NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"patientPhotoFolder"];


   NSString *imgPath = [string stringByReplacingOccurrencesOfString:@"amp;" withString:@""];

   imgPath = [imgPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

   NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

   NSString *key = [self sha1:imgPath];

   NSString *fileToSave = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",key]];


   BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileToSave];
   if (!fileExists)
   {
       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0ul);
        dispatch_async(queue, ^{
            NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgPath]];


            UIImage *image = [[UIImage alloc] initWithData:imageData];

            NSData *jpegData = UIImageJPEGRepresentation(image,4);

            [[NSFileManager defaultManager] createFileAtPath:fileToSave contents:jpegData attributes:nil];

            dispatch_sync(dispatch_get_main_queue(), ^{
               patientPhoto.image = image;
            });
        });
        patientPhoto.image = [UIImage imageNamed:@"patientPhoto.png"];
    }else{
        UIImage *image = [UIImage imageWithContentsOfFile:fileToSave];
        if(!image) image = [UIImage imageNamed:@"patientPhoto.png"];
        patientPhoto.image = image;
    }
}
4

1 に答える 1

8

Documents ディレクトリに「patientPhotoFolder」という名前のフォルダーを遅延作成する方法 (これは、たとえば次のカテゴリである可能性がありますNSFileManager

- (NSString *)pathToPatientPhotoFolder {
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                    NSUserDomainMask,
                                                                    YES) lastObject];
    NSString *patientPhotoFolder = [documentsDirectory stringByAppendingPathComponent:@"patientPhotoFolder"];

    // Create the folder if necessary
    BOOL isDir = NO;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if (![fileManager fileExistsAtPath:patientPhotoFolder
                           isDirectory:&isDir] && isDir == NO) {
        [fileManager createDirectoryAtPath:patientPhotoFolder
               withIntermediateDirectories:NO
                                attributes:nil
                                     error:nil];
    }
    return patientPhotoFolder;
}

そのフォルダの内容を削除する方法 (上のカテゴリとして実装した場合NSFileManager):

NSString *patientPhotoFolder = [[NSFileManager defaultManager] pathToPatientPhotoDirectory];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:patientPhotoFolder error:nil];
for (NSString *filename in contents)  {
    [[NSFileManager defaultManager] removeItemAtPath:[patientPhotoFolder stringByAppendingPathComponent:filename] error:NULL];
}
于 2012-12-12T11:13:05.120 に答える