这是一个很大的问题,在各种主题。
如何加载图标/图像?
UIImage *image = [UIImage imageNamed:@"fubar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
如果你只是在你的项目中的图像复制他们的主束的一部分,并且在大多数情况下,你和几乎忽略在那里它们被保存。访问它们非常简单。 (见上)
有没有一种“更简单的方法”...好吧,如果你想知道是否有更简单的方法,你必须给我们一个方法。不过,我认为一个简单的命名约定和循环是可行的。
NSMutableArray *icons = [NSMutableArray array];
NSString *iconName;
UIImage *image;
UIImageView *imageView; = [[UIImageView alloc] initWithImage:image];
for (int count=0 ; count < 65 ; count++) {
iconName = [NSString stringWithFormat:@"icon%d.png", count];
image = [UIImage imageNamed:iconName];
[icons addObject:image];
}
那么你可以使用你的icons
阵列作为UITableView
的基础。尽管如此。您可能需要考虑使用UICollectionView
代替。它就像一张桌子,但它是一排物品而不是只有一排。它可以更好地处理大量图像。
至于如何将图像复制到视图中,您的UITableView
和UICollectionView
都有他们“询问”数据并提供给您的位置的方法。根据你刚才设定的位置信息,它会处理剩下的部分。它可能看起来像
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
// ...
// remembering 'icons' is our array of image data
cell.imageView.image = icons[indexPath.row];
// ...
}
从评论编辑:How to tell if a file exists in the app bundle?(我的回答仅仅是一个剪切/粘贴)
[[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];
来源
2013-04-30 13:21:43
DBD
虽然有一个跟进问题:如何检查某个图像是否存储在应用程序包中? – MichiZH 2013-05-01 08:27:32