응용 프로그램의 문서 디렉토리로있는 NSData를 작성하고 응용 프로그램에서 데이터 읽기를 들어 응용 프로그램의 문서 디렉토리
//get the path of our apps Document Directory(NSDocumentDirectory)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//the path is stored in the first element
NSString *path = [paths firstObject];
//append the name of our file to the path: /path/to/myimage.png
path = path = [path stringByAppendingPathComponent:@"myimage.png"];
//store any errors
NSError *error;
// Write NSData to Disk
BOOL success = [imageData writeToFile:path atomically:YES];
if(success){
NSLog(@"Success");
}else{
NSLog(@"Error: %@",[error localizedDescription]);
}
에 데이터 쓰기를 들어 응용 프로그램
에 다시 데이터를 필요로 할 때 그것을 읽고 문서 디렉토리
/get the path of our apps Document Directory(NSDocumentDirectory)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//the path is stored in the first element
NSString *path = [paths firstObject];
//append the name of our file to the path: /path/to/myimage.png
path = path = [path stringByAppendingPathComponent:@"myimage.png"];
//store any errors
NSError *error;
NSData *rawImage = [NSData dataWithContentsOfFile:path
options:NSDataReadingMappedIfSafe error:nil];
if(rawImage){
UIImage *image = [UIImage imageWithData:rawImage];
NSLog(@"%@",image);
}else{
NSLog(@"Error: %@",[error localizedDescription]);
}