Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could you provide a README and/or examples for usage? #1

Open
gotmayonase opened this issue Mar 18, 2013 · 1 comment
Open

Could you provide a README and/or examples for usage? #1

gotmayonase opened this issue Mar 18, 2013 · 1 comment

Comments

@gotmayonase
Copy link

I believe this code is exactly what I'm needing. I've got annoying memory issues with a UIDocument that contains multiple photos/videos that I'm trying to fix. However I'm not certain how to implement this with my current codebase. If you could provide a small sample xcode project, I wouldn't mind writing some documentation and submit it as a pull request once I figure it all out.

@lukescott
Copy link
Owner

Sorry, I haven't had a chance to do it yet. Been busy with other projects.

Essentially you want to subclass LSDocument instead of UIDocument. In the loadFromContents and contentsFromType you basically want to use LSFileWrapper as you would an NSFileWrapper. If contents is nil from loadFromContents you want to create a directory wrapper. You can do this with -initDirectory. You can create files with -initFile and add them with -addFileWrapper or -setFileWrapper - the difference between the two being that add will suffix a filename with a 2, 3, 4, etc... if the file exists already and return the final filename - set will overwrite an existing one. -addContent and -setContent work the same way, but create the file wrapper for you.

The key differences are:

  • LSFileWrapper is mutable. So you don't have to recreate it.
  • If you want a file to be loaded, you have to set cacheFile to YES in loadFromContents. If you do that the LSDocument will call loadCache for you. If you set cacheFile to YES later you have to call loadCache yourself.
  • After a file is loaded into cache you can use various convenience methods like -data, -string, -dictionary, -image. If cacheFile is NO those methods will return nil.
  • You can update content using the updateContent call. This will accept most anything. It will even know what to do with ALAsset, image or video.

The cache is mostly for small files, like html, plist, etc... If you want to load images, I would do it with methods like [UIImage imageWithContentsOfFile].

You'll never have to call - writeUpdateToURL on the LSFileWrapper, unless you're using it outside LSDocument. You always want to call the methods you normally would on UIDocument.

There is a "savePathFirst" method in LSDocument that allows you to save a file / directory first before everything else. You use this in cases where you want to save thumbnails to load in something like a UIWebView and save larger files later. It gives you two blocks - one after the initial files are saved, and one after everything has been saved.

Another method that's useful to use is -fileWrapperWithPath. It allows you to provide a relative path inside a directory. So you can literally do something like "path/to/foo.html" and it will drill down into it to give you the file wrapper for that file. There is a -fileWrapperWithPath:create:isDirectory that will allow you to create one if it doesn't exist. I typically will use that in combination with -updateContents.

Feel free to write a README file and send me a pull request if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants