繁体   English   中英

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

[英]loading images from folder into iphone app

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

我想了解的是,确切地将图像存储在何处以及如何在代码内链接它们。

我在这里阅读了一些问题,例如( 如何将本地jpeg或png图像文件加载到iPhone应用程序中? )和网络上的其他参考文献。 我正在尝试为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之间有什么区别? 我在网络上找不到任何与此有关的利弊。 有人可以指导我什么是好的编程习惯吗?

接下来,我看了看:将图片放入Ios开发的数组中 我以为可以将磁盘上的文件夹中的图像加载到阵列中。 这将使我能够将实际文件保留在本地磁盘本身上,而无需执行在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 ares中,则在运行时会崩溃。 如果是这样,那么此代码与上一个代码相比有什么意义?

问题4:这两种方法是否都要求我们手动向项目添加图像,第二种方法是否比第一种有优势?

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

谢谢你的帮助。 抱歉,如果这已经在其他地方得到了详细解答。

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

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

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

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

问题2和问题4:当您拥有具有不同分辨率的同一图像以支持所有设备时,将使用xcassets。 如果要使用xcassets或将图像直接添加到xcode项目中,这取决于您的偏好。 您可以在此处阅读有关xcassets的更多信息

是的,您可以将非PNG图像添加到xcassests

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM