user210437
user210437

Reputation:

Move a folder to Documents directory

The iPhone application I'm working on comes bundled with 20MB of images in the application directory and I need to write new images to the documents directory over time.

Ideally I'd like to move the initial folder to the documents directory, copying the folder would mean wasting 20mb of the users disk space.

Failing that I'll create the required structure in the documents directory and leave the initial images in place, but that would mean checking in two places every time I want to display an image.

Is there a way of automagically searching the application bundle and documents directory when building an image path?

Upvotes: 0

Views: 922

Answers (2)

Jeff Kelley
Jeff Kelley

Reputation: 19071

To answer your first concern: you can’t move anything out of the application bundle. Any modification of said bundle invalidates the signature and will render your app non-functional on all but jailbroken iOS devices.

Now, onto the challenge of copying images out: To find something in the main bundle, you’d do this:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage"
                                                      ofType:@"png"];

For an image, though, you’re better off doing this:

UIImage *imageFromBundle = [UIImage imageNamed:@"MyImage.png"];

That will do caching for you in a way that loading the image directly won’t.

I don’t know why you’d want to copy the image out, but it seems that maybe you have an initial set of images that may or may not be replaced. I’d recommend, then, instead of copying out of the bundle, look for the image first in the Documents folder, and if it isn’t there, copy it out of the bundle. That allows you to replace the images without copying from the bundle or modifying the bundle.

Upvotes: 1

dredful
dredful

Reputation: 4388

AFAIK the bundle is read only. So while your "move" idea is a good one, I am fairly certain Apple does not allow for it currently.

Is there a way of automagically searching the application bundle and documents directory when building an image path?

I can say that I have done this for images but you have to create the magic yourself. I wrote a class that I would call with the name of the image as a parameter. That class would first look in the documents directory, then look in the bundle and finally look to a website for the image. When it found the image it returned the UIImage. A bit of a pain to initially setup, but once in place it is quite useful and I am glad I did it.

Upvotes: 0

Related Questions