Arrays 将图像数组获取到集合视图
我正在尝试获取文档目录中的图像数组,以便在UICollectionView中显示。获取实际图像数组时遇到问题。我花了一些时间尝试谷歌这个,但大多数人似乎是从应用程序包中获取他们的图像。我需要我的图像来自文件 以下是我尝试使用的代码:Arrays 将图像数组获取到集合视图,arrays,swift,uicollectionview,uiimage,Arrays,Swift,Uicollectionview,Uiimage,我正在尝试获取文档目录中的图像数组,以便在UICollectionView中显示。获取实际图像数组时遇到问题。我花了一些时间尝试谷歌这个,但大多数人似乎是从应用程序包中获取他们的图像。我需要我的图像来自文件 以下是我尝试使用的代码: func getImageArray() { let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory let nsUserDomainMask
func getImageArray() {
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first
{
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Images")
let image = UIImage(contentsOfFile: imageURL.path)
imageArray.append(image)
print(imageURL)
}
}
我对斯威夫特很陌生,所以答案可能是显而易见的。有人能给我指出正确的方向或告诉我我做错了什么吗?你可以尝试读取目录内容,然后分别循环从URL创建图像
do {
let allImages = URL(fileURLWithPath: dirPath).appendingPathComponent("Images")
let directoryContents = try FileManager.default.contentsOfDirectory(atPath: allImages.path)
for con in directoryContents {
let img = URL(fileURLWithPath:allImages).appendingPathComponent(con)
let image = UIImage(contentsOfFile: img.path)
imageArray.append(image)
}
}
catch {
print(error)
}
您尝试从指向字典的URL创建UIImage。好的,那么我需要一个URL数组,它将指向我目录中的每个图像?对吗?