Does NSFileWrapper load everything into memory?

青春壹個敷衍的年華 提交于 2019-11-29 07:48:32

It's not totally clear from the documentation, but I'm 99% positive that NSFileWrapper will memory-map everything. The main use case for NSFileWrapper is to embed files inside documents, where you'd presumably need access to said file at all times.

What are you trying to use the file wrapper for? I assume if you're mapping an entire directory it's not necessarily to embed media inside a document, but perhaps I'm mistaken. If you maybe talk a bit more about your use case, as that will influence what alternatives you might go for.

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];
            }        

        }
//...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!