我有一个iOS应用程序,有时需要在本地存储UIImage对象。
我使用[UIImagePNGRepresentation(image) writeToFile:full_path options:NSAtomicWrite error:nil];
保存图片,[file_manager removeItemAtPath:full_path error:NULL];
删除文件。
这一切都很好,但是,每当我删除一个文件时,我是否应该决定保存一个新文件(恰好与旧文件同名),保存代码不起作用,返回以下错误:
:ImageIO:CGImageReadCreateDataWithMappedFile' open'失败 error = 2(没有这样的文件或目录)
:ImageIO:CGImageReadCreateDataWithMappedFile' open'失败 error = 2(没有这样的文件或目录)
:ImageIO:PNG zlib错误
因此,我删除旧文件后,为什么我不能保存与旧文件同名的文件?
我问这个的原因是,我的应用程序将保存某些图像文件,然后当不再需要它们时,我的应用程序将删除它们。但是,有时我的应用程序需要再次使用图像文件(可能是删除后几个小时或几周)。发生这种情况时,我的应用程序将加载适当的图像数据,然后尝试保存它。那就是错误发生的时候。
这里出了什么问题?
谢谢你的时间,Dan。
更新 - 以下是我设置的用于保存/访问和删除图像文件的方法
-(void)save_local_image:(UIImage *)image :(NSString *)file_name {
// Get the app documents directory link.
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// Add the new file name to the link.
NSString *database_link = [[NSString alloc] initWithString:[documents stringByAppendingPathComponent:file_name]];
// Save the image data locally.
[UIImagePNGRepresentation(image) writeToFile:database_link options:NSAtomicWrite error:nil];
}
-(UIImage *)get_local_image:(NSString *)file_name {
// Create the return data.
UIImage *image_data = nil;
// Get the app documents directory.
NSArray *directory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] error:NULL];
// Only check for data if at least
// one file has been saved locally.
if ([directory count] > 0) {
// Loop through the different local files.
for (NSString *path in directory) {
// Get the full local file URL.
NSString *full_path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:path];
// Get the range of the file name.
NSRange range = [full_path rangeOfString:file_name];
// Get the image data if it exists.
if ((range.location != NSNotFound) || (range.length == [file_name length])) {
// Load the image file in.
image_data = [UIImage imageWithContentsOfFile:full_path];
break;
}
}
}
return image_data;
}
-(void)delete_local_image:(NSString *)file_name {
// Get the app documents directory.
NSArray *directory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] error:NULL];
// Only check for data if at least
// one file has been saved locally.
if ([directory count] > 0) {
// Loop through the different local files.
for (NSString *path in directory) {
// Get the full local file URL.
NSString *full_path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:path];
// Get the range of the file name.
NSRange range = [full_path rangeOfString:file_name];
// Delete the local image data if it exists.
if ((range.location != NSNotFound) || (range.length == [file_name length])) {
NSError *testError = nil;
// Delete the image file.
NSFileManager *file_manager = [[NSFileManager alloc] init];
BOOL success = [file_manager removeItemAtPath:full_path error:&testError];
NSLog(@"%d", success);
if (testError != nil) {
NSLog(@"%@", testError.localizedDescription);
}
break;
}
}
}
}
答案 0 :(得分:0)
我用它来保存并阅读:
const initialState = {
graphDatas: [],
friendsList : {},
comments: []
}
function addData(state = initialState.graphDatas, action) {
switch(action.type) {
case 'RECIEVE_DATA':
var clone = _.cloneDeep(state);
var resultPopOff = clone.pop()
let stateResult = _.isEqual(resultPopOff, action.data)
if (stateResult) {
return state
} else {
return [
...state, action.data
]
}
default:
return state;
}
}
const payment = (state = initialState, action) => {
switch(action.type) {
default:
return {
graphDatas: addData(state.graphDatas, action)
}
}
}
const rootReducer = combineReducers({
payment, routing: routerReducer
})
export default rootReducer