Does NSFileWrapper load everything into memory?

前端 未结 2 1314
心在旅途
心在旅途 2020-12-16 05:10

Lets say I have an NSFileWrapper directory. This directory is made up of several levels of directories and files. Some of the files are large. Are all these fil

2条回答
  •  Happy的楠姐
    2020-12-16 05:37

    I've made an app a while ago that generates a video. This video was then saved to a specific file format using a UIDocument subclass.

    The only way to make the app not run out of memory while executing contentsForType:error: was to output the video to a file in the tmp dir and init the filewrapper with the url to the video with NSFileWrapperReadingWithoutMapping-option to prevent it from loading the video to memory and just copy in the file.

    - (id)contentsForType:(NSString *)typeName error:(NSError **)outError {
        if (self.fileWrapper == nil) {
            self.fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
        }
    
            if (self.videoURL != nil) {
    
                NSError *fileReadError;
                NSFileWrapper *videoFileWrapper = [[NSFileWrapper alloc] initWithURL:self.videoURL options:NSFileWrapperReadingWithoutMapping error:&fileReadError];
    
                if(fileReadError){
                    NSLog(@"File read error: %@", [fileReadError localizedDescription]);
                }else {
                    [videoFileWrapper setPreferredFilename:@"video.mov"];
                    [self.fileWrapper addFileWrapper:videoFileWrapper];
                }        
    
            }
    //...
    }
    

提交回复
热议问题