我有一个 tableView,对于每个单元格,我得到一个图像 url 字符串,并且我将所有数据保存在 NSDocument 目录中。
我想将所有数据存储在 NSDocument 目录中创建的文件夹中,并且我还想稍后删除该文件夹中的所有内容。怎么做?
这是我正在使用的方法
(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;
}
}