Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

IOS app - large number of png images

I'm making a IOS app which will contain a large number of png files (200 to begin with, then more will be added over time). What is the best way to store these files if the app is to be used online, and what is the best way to store them if the app is to be used offline. Would really also appreciate some information on how the procedure should be done.

Thanks!

2 Answers

Hi Ioanna,

I am currently working on something similar myself. And im sitting here kindoff swinging between two options.

This is like a webstore app.

1) I can add all the starting images into the app and deploy the app with the images, and only download the missing ones if more products are added. The products, image name, info e.e is retrieved from a JSON file. Im storing this file on the iOS device, so that i can use it for offline as well - When each item is displayed in a tableViewController, it triggers a small script that checks if i have the image downloaded or if i need to download it from the server. if it needs to download it, it displays a "image not available" until the asynchronus task of downloading the image is done. (Usually this is so fast that i dont see the "Not available image".

  • This method is so that i can download the item list more frequent without using too much bandwith and leaving the user with a blank screen for a period of time.

2) Same thing as above, only that instead of checking for new images when scrolling trough the items, i make the application check with the server if the file i have locally is the same as on the server (by timestamp) - Running this task like 2 times per day and/or on application launch. If there is an update available i want to provide the user with a notification (small banner on top) saying that there is a update available - When the user clicks on the banner, it shows a progress bar for downloading the new .json file and it loops trough all the items and checks to see if i have the image for it, if not - add it to an Array, and calculate the filesize of all images, and download the images 1 by 1, and show a progressbar based on downloaded bytes vs total byte size of all images.

When downloading stuff simple stuff without the progress bar, take a look at the NSData in the Documentation. I still need to lookup how i can do 2).

Example on how i download the a image when checking for it in the tableView.

+(void)downloadImage:(NSString*)imageName
{
    NSLog(@"Downloading Image %@",imageName);
   NSString* imageURL = [NSString stringWithFormat:@"%@/%@",@"http://myurl.com/myimages",imageName];
NSURL *URL = [[NSURL alloc] initWithString:imageURL];
NSError* test;

NSData *imageData = [[NSData alloc] initWithContentsOfURL:URL options:0 error:&test];
if (imageData)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:appFile atomically:YES];
    NSLog(@"Image Saved to: %@",appFile);
} else {
    NSLog(@"Could not Download image");
    NSLog(@"%@",test);
}

}

Hope this helps!

Hi Christer,

Thanks for your reply - I'm really new to ios app development and am kind of struggling along with this app I'm making!

So from what I understand I guess the images have to be stored somewhere on a server and then can be retrieved via a JSON file within the app, and this way the images can be downloaded even when the app is offline?

No it cant be downloaded when offline. What You can do is drag all the images into your xcode project, that will make sure the images are available, allways :)