将图像从文件夹加载到iPhone应用程序

时间:2014-10-22 23:26:17

标签: ios iphone xcode image

我是Ios编程的新手,正在尝试构建一个简单的配方应用程序。为此,我有一个表格格式的食谱,我想为每一个包含一张图片。我正在使用Xcode 5.1并在Iphone 5尺寸上定位Ios 7(我不知道它是否重要)。

我想要了解的是,在哪里存储图像以及如何在代码中链接它们。

我在这里阅读了一些问题,如(How do you load a local jpeg or png image file into an iPhone app?)和网上的其他参考资料。我正在尝试为appcoda做SimpleTable示例。这就是我在代码中所拥有的:

⁃   @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;
⁃   }

这很有效。但我想了解一些事情。

Q1。图像可以驻留在我的主项目目录中的文件夹中吗?或者这些图像是否必须在Xcode中加载到项目中?它只有在我将它们拖动到项目文件区域的左侧时才会起作用(附带截图)。

Q2。将图像添加到项目文件的此部分中,将它们添加到Images.xcassets中有什么区别?我无法在网上找到任何具体的利弊。有人可以指导我什么是好的编程习惯吗?

接下来,我看了一下:Pictures put into a array Ios developing。我以为我可以将磁盘上的文件夹中的图像加载到数组中。这将使我能够将实际文件保留在本地磁盘本身上,而无需通过在Xcode中添加对它们的引用的额外步骤。我认为它会在运行时生成对物理文件的动态引用。但我想我错了:

⁃   @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。这会构建,但如果图像尚未添加到项目文件区域或xcassets中,则会在运行时崩溃。如果是这样,这个代码与前一个代码的重点是什么?

问题4:这两种方法都要求我们手动将图像添加到项目中,第二种方法的优势是否优于第一种?

Q5。最后,如果将图像添加到xcassets区域是一个好习惯,那么它们必须是png文件吗?我不能在那里添加任何jpg文件。用户界面拒绝识别它们。

感谢您的帮助。如果在其他地方已经详细回答了这个问题,请道歉。

PS。我还无法发布图片,但已在此处上传:

http://i.stack.imgur.com/OWt42.png

1 个答案:

答案 0 :(得分:0)

Q1:是的,它们可以位于主项目的任何文件夹中。但是您必须将这些文件添加到您的xcode项目中,以将它们包含在您的应用中。

问题3:您无法从光盘中加载图像。您的应用无法访问您的本地光盘。您的IOS应用程序是沙箱,并且无法访问该沙箱区域之外的任何其他资源。请阅读此苹果文档https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Q2&amp; Q4:当您拥有具有不同分辨率的相同图像以支持所有设备时,将使用xcassets。如果您想使用xcassets或直接将图像添加到xcode项目,这取决于您的偏好。 您可以阅读有关xcassets here

的更多信息

Yes you can add non-PNG images to xcassests

相关问题