Where does `[UIImage imageNamed:]` load images from and how can I, at runtime, insert images into there?
ios, iphone, nsdata, objective-c, uiimage
Solution
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.
Problem
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