I am new to Ios programming and am trying to build a simple recipe app. For this, I have a se of recipes in a tabular format and I want to include a picture for each of them. I am using Xcode 5.1 and target Ios 7 on an Iphone 5 size (I don't know if it matters).
What I am trying to understand is, where exactly to store the images and how to link them inside the code.
I read some questions here like (How do you load a local jpeg or png image file into an iPhone app?) and other references on the web. I am trying to do the SimpleTable example for appcoda. This is what I have in the code:
⁃ @implementation SimpleTableViewController
⁃
⁃ NSArray *recipes;
⁃ NSArray *thumbnails;
⁃
⁃ - (void)viewDidLoad
⁃ {
⁃ [super viewDidLoad];
⁃ // Do any additional setup after loading the view, typically from a nib.
⁃
⁃
⁃ // Initialize thumbnails
⁃ thumbnails = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"10.jpg", @"11.jpg", @"12.jpg", @"13.jpg", @"14.jpg", nil];
⁃
⁃
⁃
⁃ }
⁃
⁃ - (void)didReceiveMemoryWarning
⁃ {
⁃ [super didReceiveMemoryWarning];
⁃ // Dispose of any resources that can be recreated.
⁃ }
⁃
⁃ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
⁃ {
⁃ return [recipes count];
⁃ }
⁃
⁃ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
⁃ {
⁃
⁃ static NSString *simpleTableIdentifier = @"SimpleTableCell";
⁃
⁃ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
⁃
⁃
⁃ cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
⁃ cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
⁃
⁃
⁃ return cell;
⁃ }
This works. But I am trying to understand a few things.
Q1. Can the images reside in a folder in my main project directory? Or do these images have to be loaded into the project within Xcode? It only works when I drag them images I have into the left side of the project files area (screenshot attached).
Q2. What is the difference between adding images into this section of the project files, vs adding them into the Images.xcassets? I couldn't find any specific pros or cons on the web regarding this. Could someone guide me as to what is good programming practice?
Next, I looked at this: Pictures put into a array Ios developing. I thought I could load images from a folder on my disk into an array. This would enable me to keep the actual files on my local disk itself, without having to go thru the extra steps of adding references to them within Xcode. I thought it'd generate dynamic references to physical files during runtime. But I guess I was wrong:
⁃ @implementation SimpleTableViewController
⁃
⁃ NSArray *recipes;
⁃ NSArray *thumbnails;
⁃
⁃ - (void)viewDidLoad
⁃ {
⁃ [super viewDidLoad];
⁃ // Do any additional setup after loading the view, typically from a nib.
⁃
⁃
⁃ }
⁃
⁃ - (void)didReceiveMemoryWarning
⁃ {
⁃ [super didReceiveMemoryWarning];
⁃ // Dispose of any resources that can be recreated.
⁃ }
⁃
⁃ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
⁃ {
⁃ return [recipes count];
⁃ }
⁃
⁃ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
⁃ {
⁃
⁃ static NSString *simpleTableIdentifier = @"SimpleTableCell";
⁃
⁃ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
⁃
⁃ NSArray *PhotoArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"."];
⁃
⁃ NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:PhotoArray.count];
⁃
⁃ for (int i = 1; i <= 14; i++) {
⁃ NSString *filename = [NSString stringWithFormat:@"%d", i];
⁃ NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"jpg" inDirectory:@"."];
⁃ //UIImage *image = [UIImage imageWithContentsOfFile:path];
⁃
⁃ [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
⁃ }
⁃
⁃
⁃ if (cell == nil) {
⁃ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
⁃ }
⁃
⁃ cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
⁃ //cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
⁃ cell.imageView.image = [imgQueue objectAtIndex:indexPath.row];
⁃
⁃ return cell;
⁃ }
Q3. This builds, but crashes upon run if the images have not been added to either the project files area, or the xcassets ares. If so, what is the point of this code over the previous one?
Q4: Is both approaches require us to manually add images to the project, is there any advantage of the second over the first?
Q5. Lastly, if it is good practice to add images into the xcassets area, do they have to be png files? I cannot add any jpg files in there. The UI refuses to recognize them.
Thanks for your help. Apologies if this has already been answered in detail somewhere else.
ps. I can't post images yet but its been uploaded here: