【发布时间】: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。我还不能发布图片,但它已上传到这里:
【问题讨论】:
-
如果您有服务器,您可以从项目或网络服务加载图像。通过使用 XML/JSON 解析。