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

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.
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)
How do I create a tree structure directory to store resources?
How do I add files to the created directory?
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.
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?
So called "resource bundle" refers to the "bundle" used in above
questions?
Thank you.

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.

Related

How can we make a nested directory in resource folder to put the xmls with same name?

How can we make a nested directory in resource folder to put the xmls with same name for different folders.And how we can read it using path in iphone app programatically?
Actually i want to make like resource>a>b>some.xml
again in resource like resource>f>g>some.xml
and so on...how ever both xml is containing different data in it.and also tell me the way how we can read it in iphone application.
You cannot use the same filename, even if they resides in different folders.
Instead, as a workaround, you can name the files like:
a_b_some.xml
f_g_some.xml
you can create a folder structure on your system and then directly add the folder to your XCode Project under resources by drag-drop.
this folder must display in BLUE color (physical grouping in folder on disk) rather than YELLOW (logical grouping in project only) just like native folders on mac. like this you can put same file in different folder hierarchy without any problems.
you need to create a absolute path to your file whenever you want its access your file [NSBundle pathForResource:] will not work in this case as it does not have access to your custom folder hierarchy.
best of luck.

Shipping Documents Items with an iPhone App

My iPhone app uses a small database to store its settings and saved files. How can I ensure that the default database, with its default settings, gets distributed to anyone who downloads it along with the application files?
EDIT Sorry, I was in a rush when I posted this. I forgot to mention that the database needs to end up in the 'Documents' folder of the application so that it can be backed up at a later date by the user.
-Ash
Put it in "Resources". Then on your first launch, you'll need to load that file out of your mainBundle and save it to the Documents directory. The file will "come with" the app, but it won't live in the right place to get caught by backup.
A side-effect is that restoring the app to factory settings is as easy as deleting that file. Next launch, you can see you don't have your file, and copy a fresh one out of your bundle.
You include it as a file in the Resources folder of your application.

Managing resources and keeping them out of version control

My iPhone app will have a map with about 10 points on it. It will play sound files based on the proximity to those points.
What's the best way of managing these resources?
Coordinates and accompanying sound file could be stored in a plist, as an array of dicts with latitude, longitude and file name. Then the sound files could be stored separately.
The plist and sound files should not be version controlled.
How much, and what, should happen compile-time, and run-time? How do I manage this?
The end result should be an app binary with the sound files embedded and the records stored in core data. Doing stuff on first launch of app is also OK.
I think my question could be rephrased to: how do I manage resources programatically, at build time?
Add the sounds folder as a 'folder reference'. Then any files you add to that folder will be included in your bundle. Right click on your project->Add Existing Files then choose 'Create Folder References'

High level process of extracting images from a container

Right, this is the problem I have a container (rar,zip) which contains images png's tiffs bmps or jpegs in an order.
The file extension isnt zip or rar though but uses the same compression.
I want to pull out a list of images contained within the file in the numerical order, then depending on the user decision go to the image selected.
I'm not after any code just the high level thought process/logic of how this can be achieved and how it could be achieved on iphone OS.
From what i know of iphone OS it uses a kind of sandbox environment so how would this effect the process as well.
Thanks
You can include the libz framework in your project and write some C to manage zipped data. Or you can use Objective-C wrapper classes others have written.
Your application resides in its own sandbox. You can include zip files in the "bundle", i.e. add them to your project, and copy them to the application's Documents folder to work with them. Or you can copy archived data over the network to the application's Documents folder if you don't want to include files in your project.
I don't think the extension matters so much as the data being in the format you expect it to be.
Everything I wrote above is for zip-ped files. If you're working with rar-formatted archives, you'll need to look at making a static library for the iPhone, perhaps from the UnRAR source code.

Copying iPhone CoreData files from one project to another

I'm trying create 2 apps, one that builds a persistent store, and another one that consumes it.
So far I have built one app that uses CoreData to successfully build a database from an XML file. So this project contains the data model, the .h/.m files for the entities, etc.
I'm now trying to enable the second app to read that .sqlite file by copying the data model file, the .h/.m files related to the entities and the sqlite file to that project (via add existing).
The code executes but always fails to find any objects in the database.
Are there any restrictions or correct steps to take when trying to copy over these files?
The solution here is deceptively simple.
Just copy your .xcdatamodel file from one project to another and then when you run your app in the simulator for the first time it'll create a Documents folder for the app. Just drop your saved .sqlite or .binary files into the yourApp/Documents directory on the device.
You can find the simulator directory in "~user/Library/Application Support/iPhone Simulator".
You can also download, edit, and upload the myApp directory on a provisioned iPhone by dragging and dropping into and out of the Organizer. Look at the Applications list.
The iPhone doesn't support xml stores with core data, only sqlite or atomic (binary). The sqlite store is by far the better option for most applications since it doesn't all have to be loaded into memory at runtime.
Is this what you meant?
I think (not 100 percent sure) in your app plist, if you set your application bundle name to the same thing, they will share resources because the device will think they are the same application...
I don't think this is going to work the way that you want it to. On the iPhone, each application runs in its own "sandbox", and it's not really possible for one application to write files that another can read.
Is it really two different projects, or is it two targets in the same project? That would seem to make a lot more sense, and then you can share entity objects as they change.
For copying core data files from one project to another, I first created a new project with core data support and then I opened the contents of the previous coredata file and except the root tag, pasted all child tags in the new core data file in new project.
Previousy I tried to delete the coredata file in new project, copy pasted the previous one and changed its name and it was not working.