7

(1)txtファイルと(2)いくつかのjpg画像で構成したいUIDocumentがあります。txt とすべての jpg を 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) {}];
}];
4

1 に答える 1

3

You should keep a reference to your NSFileWrapper in your UIDocument instance. This way, only the changed contents will be rewritten and not the whole wrapper.

So, keep a reference when you load a file (or create a new one for new documents) :

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError {
    // save wrapper:
    self.fileWrapper = (NSFileWrapper*)contents;

now you only have to update the wrapper if your file actually changed:

- (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:…

I know the code is very brief, but I hope that helps to point you in the right direction.

于 2013-02-21T02:54:37.220 に答える