(iphone) How to store images in directory structure (vs flat documents directory) and related questions

流过昼夜 提交于 2019-12-13 02:37:05

问题


I'm looking for a good way to manage many image files in my app.
When the number of images to maintain gets large, it seems vital to have a directory structure to hold them.(to organize images and to support duplicate image names)

Since normal way of storing images in documents directory(by adding images to xcode's resource folder) doesn't seem to support directory structure,
I probably need to use something called bundle.

Here are questions.

  1. It's not clear to me what's the difference between "documents directory" and "bundle". Are they just names to specify directory path inside an iphone application? Since documents directory doesn't support directory structure, I guess it's not a regular file path in iOS? (I'm looking for a definition or overview description on those two terms enough to help answering the following questions)

  2. How do I create a tree structure directory to store resources?

  3. How do I add files to the created directory?
  4. What is the step (probably in xcode) to add new files to the directory as project grows? note: question 3 deals with initial set up, 4 deals with update.

  5. What happens to files under documents directory and bundle when user updates the app? Since many applications store user data somewhere, there must be a way of updating an app without wiping out the saved user data. ie. How does "documents directory" and "bundle" behave in update situation?

  6. So called "resource bundle" refers to the "bundle" used in above questions?

Thank you.


回答1:


Your app is a "bundle". It's represented by an NSBundle object. On iOS, this is the only bundle you are allowed to use; you can't create another bundle and import it into your app.

However, you can add a subdirectory to your app's bundle. In Xcode 4, select your project in the navigator and click on the Build Phases tab. Add a new build phase (bottom right), Copy Files. Move it up just below Copy Bundle Resources, and rename it something meaningful ("copy interface images", or something). You'll notice you've got a Subpath field there - that's your directory in your bundle. Drag the files you want in that subdirectory on to this build phase, and then you can access them through the normal methods in NSBundle, UIImage, NSData and so on.

Wish it was easier? Me too.



来源:https://stackoverflow.com/questions/5673389/iphone-how-to-store-images-in-directory-structure-vs-flat-documents-directory

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