【问题标题】:loading images from folder into iphone app将文件夹中的图像加载到 iphone 应用程序中
【发布时间】:2014-12-18 13:48:17
【问题描述】:

我是 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;
⁃   }

这行得通。但我想了解一些事情。

第一季度。图像可以驻留在我的主项目目录中的文件夹中吗?或者这些图像是否必须在 Xcode 中加载到项目中?只有当我将它们的图像拖到项目文件区域的左侧时它才有效(附截图)。

第二季度。将图像添加到项目文件的这一部分与将它们添加到 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;
⁃   }

第三季度。这会构建,但如果图像尚未添加到项目文件区域或 xcasset 区域,则会在运行时崩溃。如果是这样,这段代码与前一个代码相比有什么意义?

Q4:这两种方法都需要我们手动将图像添加到项目中,第二种方法比第一种方法有什么优势吗?

Q5。最后,如果将图像添加到 xcassets 区域是一种好习惯,它们是否必须是 png 文件?我无法在其中添加任何 jpg 文件。 UI 拒绝识别它们。

感谢您的帮助。抱歉,如果这已经在其他地方详细回答。

ps。我还不能发布图片,但它已上传到这里:

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

【问题讨论】:

  • 如果您有服务器,您可以从项目或网络服务加载图像。通过使用 XML/JSON 解析。

标签: ios iphone xcode image


【解决方案1】:

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

Q3:您无法从光盘加载图像。您的应用程序无权访问您的本地磁盘。您的 IOS 应用程序已被沙盒化,它无法访问该沙盒区域之外的任何其他资源。请阅读此苹果文档https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

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

Yes you can add non-PNG images to xcassests

【讨论】:

  • 感谢您详细回答 Priyatham51。如果必须将所有图像添加到项目中,为什么需要第二种方法?为什么不像第一种方法那样使用“thumbnails”数组的声明呢?此外,第二种方法具有“inDirectory”。如果图像在任何情况下都是项目的一部分,它指的是什么?另外,像 facebook 这样的应用程序如何从网络加载动态图像(我假设它们是从网络上下载并本地存储在应用程序的沙箱目录中的。我可以在这里模拟吗?)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 2012-09-03
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多