我有一个UIDocument,我希望它由(1)一个txt文件和(2)几个jpg图像组成。我将txt和所有jpgs放入一个NSFileWrapper中。
当我加载UIDocument时,我非常快地需要txt文件中的信息,所以我首先加载它,然后忽略所有的图像,直到我真正需要它们。
虽然我知道如何懒惰地加载映像,但我不知道如何“懒散地”保存映像(特别是在使用iCloud时,我不希望文件不必要地上传/下载)。让我们假设我已经加载了所有的图像,并且没有改变它们。然后,我想保存UIDocument,忽略所有的图像(因为它们没有改变),但想保存文本,因为它确实改变了。
,我将如何实现这一点?有可能吗?还是自动完成的?或者我不应该把图像放在我的UIDocument中,让每个图像被一个不同的UIDocument处理?恐怕这对我来说有点混乱。
到目前为止,这是我的代码,它将保存所有图像和文本(不管它们是否被更改):
UIDocument
-(id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
NSMutableDictionary *wrappers = [NSMutableDictionary dictionary];
// the following puts a wrapper into a dictionary of wrappers:
[self encodeObject:self.text toWrappers:wrappers toFileName:@"text.data"];
[self encodeObject:self.photos toWrappers:wrappers toFileName:@"photos.data"];
NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];
return fileWrapper;
}当我想保存UIDocument时:
[self.doc saveToURL:self.doc.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
[self.doc closeWithCompletionHandler:^(BOOL success) {}];
}];发布于 2013-02-21 02:54:37
应该在NSFileWrapper实例中保留对UIDocument的引用。这样,只有修改过的内容才会被重写,而不是整个包装。
因此,在加载文件(或为新文档创建新文件)时保留一个引用:
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError {
// save wrapper:
self.fileWrapper = (NSFileWrapper*)contents;现在,您只需在文件实际更改时更新包装器:
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError {
NSFileWrapper *subwrapper = [self.fileWrapper.wrappers objectForKey:@"subwrapper"];
if(self.somethingChanged) {
[self.fileWrapper.wrappers removeFileWrapper:subwrapper];
subwrapper = [[NSFileWrapper alloc] initRegularFileWithContents:…我知道代码非常简短,但我希望这能帮助您指出正确的方向。
https://stackoverflow.com/questions/10983903
复制相似问题