4

I have a bunch of code that's already written which currently relies on images being distributed with my app. I want to change this so that images can be downloaded from a website on the fly to prevent constantly having to upload/get approved/release new versions.

The code has [UIImage imageNamed:@"MyImageName.jpg"] type references all over it. I have identified a section of code which could happily download these images so that if I could get them in the right place, my app will work fine, providing there's a network connection, downloading images on the fly. However I don't know where to write the downloaded image data to, in order for my pre-exisiting code to function unchanged.

I have code which gets the relevant image data into an NSData object - so just need to know where to write this data to?

    NSLog(@"ERROR - MISSING IMAGE: %@", imageName);
    NSData* theImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://f6c.yourmapp.mobi/Images/%@", imageName]]];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* localFilePath = [documentsDirectory stringByAppendingPathComponent:imageName];
    [theImage writeToFile:localFilePath atomically:YES];
    tempImage = [UIImage imageNamed:imageName];
    if (tempImage != nil)
    {
        [images addObject:tempImage];
        haveAddedImages = YES;
    }

Thanks

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Matt Fellows
  • 6,512
  • 4
  • 35
  • 57

3 Answers3

6

It loads the images from the application bundle. You cannot write to this folder at runtime. Instead write your image into the Documents folder (you can obtain the path with NSDocumentDirectory) and use [UIImage imageWithContentsOfFile:] to load such an image.

Costique
  • 23,712
  • 4
  • 76
  • 79
kuba
  • 7,329
  • 1
  • 36
  • 41
  • 1
    As `[UIImage imageNamed]` automatically chooses retina files (if they're part of the bundle), you might want to create some custom method to do the same when loading images from the documents directory. – Wolfgang Schreurs Apr 16 '12 at 13:39
  • This is not strictly true. it depends on your goal - ie replace deployed images displayed in a view. you can load your nib from different bundle path, and from the point of view of loading images in nibs that replace the application bundle images of the same name, this works.one small problem: if you have previously loaded an image name from the main bundle, you will get the main bundle image, not the new one. but: load the nib before app launch and never load those original images and, it works. tip: create symlinks back to app bundle for the nibs and unchanged images. problem: restart needed – unsynchronized Aug 29 '12 at 22:39
1

You can write images only to the documents folder of the application.

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

But the code [UIImage imageNamed:@"MyImageName.jpg"] loads image from Your application bundle, You cant write data to it.

So you can download and save images to the documents folder mentioned above, and use the imagename and recreate the path when you need it.

use + (UIImage *)imageWithContentsOfFile:(NSString *)path instead to load from path.

J S Rodrigues
  • 471
  • 4
  • 8
0

You can download the images into the documents directory. Then load the images from the documents directory

here are some links which may help you achieve this.

http://www.iphonedevsdk.com/forum/iphone-sdk-development/79393-how-download-files-server.html

http://www.cocos2d-iphone.org/forum/topic/16121

Iphone : How to Display Document Directory images in Image View?

Community
  • 1
  • 1
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44