寫您的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]);
}