簡體   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