已尝试将我NSMutableArray
的对象存储到,NSUserDefaults
但没有运气。我NSMutableArray
在此处包含此日志:
`ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=92A7A24F-D54B-496E-B250-542BBE37BE8C&ext=JPG`
我知道它是一个ALAsset object
,在AGImagePickerController
它被比较为 NSDictionary,所以我需要做的是保存 NSDictionary 或我用来存储我的数组,ALAsset object
然后将其保存在文件中NSDocu
或NSCaches
文件中,然后再次检索它(这是我的主意)。
但问题是,虽然我尝试了这段代码但没有工作,也没有在NSDocu
或NSCache
目录中显示任何内容。
第一次尝试(info
是包含的那个ALAsset object
):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *filePath = [basePath stringByAppendingPathComponent:@"filename.plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:filePath];
NSString *error;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData) {
[info writeToFile:filePath atomically:YES];
} else {
NSLog(error);
}
第二次尝试:
- (NSString *)createEditableCopyOfFileIfNeeded:(NSString *)_filename {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableFilePath = [documentsDirectory stringByAppendingPathComponent: _filename ];
success = [fileManager fileExistsAtPath:writableFilePath];
if (success) return writableFilePath;
// The writable file does not exist, so copy the default to the appropriate location.
NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: _filename ];
success = [fileManager copyItemAtPath:defaultFilePath toPath:writableFilePath error:&error];
if (!success) {
NSLog([error localizedDescription]);
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
return writableFilePath;
}
以这种方式保存它:
NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"hiscores"]];
if (![info writeToFile:writableFilePath atomically:YES]){
NSLog(@"WRITE ERROR");
}
第三次尝试:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:??????];
[info writeToFile:filePath atomically:YES];
第四次尝试(不确定,因为它在 appbundle 中进行了修改): https ://stackoverflow.com/a/6311129/1302274
还有其他方法吗?希望有人指导我。