UICollectionView
是要走的路。
您需要先载入所有本地头像文件名。以下示例将加载以avatar-
开头的app目录中的所有图像,忽略所有保留@2x.png
文件。
func getAvatarFilenames() -> Array<String> {
var avatarFileNames = Array<String>()
var paths = NSBundle.mainBundle().pathsForResourcesOfType("png", inDirectory: nil)
for path in paths {
var imageName = path.lastPathComponent
// ignore retina images as when the uiimage loads them back out
// it will pick the retina version if required
if (imageName.hasSuffix("@2x.png")) {
continue
}
// only add images that are prefixed with 'avatar-'
if (imageName.hasPrefix("avatar-")) {
avatarFileNames.append(imageName)
}
}
return avatarFileNames
}
然后,您可以创建一个加载每个头像文件名的UICollectionView
。像这样配置每个单元格(假设您的AvatarCell
具有标签1000
的图像 - 或更好,UICollectionViewCell
子类)。
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("AvatarCell", forIndexPath: indexPath) as UICollectionViewCell
var avatarImageView = cell.viewWithTag(1000) as UIImageView
avatarImageView.image = UIImage(named: avatarFileNames[indexPath.row])