swift - 在 ios Swift 中的 Images.xcassets 文件夹中的 Collection View 中显示图像

标签 swift uiimageview uicollectionview

我在 Images.xcassets 中有一个图像文件夹。我只想显示该特定文件夹中的所有图像,并将这些图像加载到 Collection View 中。我知道我将不得不编写这段代码来显示图像

imageView.image = UIImage(named: "Apple")

我将不得不运行一个 for 循环来显示 Collection View 中的所有图像。但是我不知道如何访问 Images.xcassets 文件夹中的所有图像

最佳答案

嗯,按照我的理解,Assets.xcassets文件夹里面的一个文件夹是没有意义的。如果有一个名为 JackApple 的文件夹,那么在 Jack 中你有 Apple。然后您尝试运行 imageView.image = UIImage(named: "Apple"),是的,这会起作用。你不需要 imageView.image = UIImage(named: "Jack/Apple")

因此,如果您只是将图像命名为 image1image2image3image4image5 然后你可以做类似的事情。

for index in 0 ..< 5 {
    imageView.image = UIImage(named: String(format: "image%i", index))
}

图像在文件中的位置并不重要。

关于swift - 在 ios Swift 中的 Images.xcassets 文件夹中的 Collection View 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39096055/

相关文章:

ios - 检索附加到 currentUser/Parse 的照片

ios - 如果 CollectionView 项目集成了另一个 CollectionView,则无法选择该项目

ios - Swift:堆栈 View 不显示完整的 subview

swift - 如何编写一个扩展来支持 [String] 和 [Substring]?

ios - 基于窗口的应用程序或基于导航的应用程序

ios - 如何从 CollectionView 中的特定索引开始

ios - 如何在 2 个不同的 UICollectionViewFlowLayout 实例之间切换?

ios - Mapbox iOS SDK - visibleFeaturesAtPoint 返回空数组

ios - 有没有办法以编程方式创建和使用展开转场?

ios - 通过文件名更改 UIImageView 的颜色