订购一个 NSURL 数组
Order a NSURL array
我正在将大量图像路径加载到 NSURL 中。
这些图像位于从 1.PNG、2.PNG、3.PNG 到 1500.PNG 排序的文件夹中。
当我尝试加载它们时:
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
print(url)
let fileManager = NSFileManager.defaultManager()
let properties = [NSURLLocalizedLabelKey,
NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
do {
imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.SkipsHiddenFiles)
} catch let error1 as NSError {
print(error1.description)
}
imageURLs 数组填充:
imageURLs[0] = ...[=11=].PNG
imageURLs[1] = ....PNG
imageURLs[2] = ...0.PNG
imageURLs[3] = ...00.PNG
而且不是数字顺序!
有人可以帮助对 imageURL 进行排序,或者当我在其上加载图像路径时或加载后?
因为你想按数字对文件进行排序,你必须首先解析实现它的路径,所以假设我们有以下 NSURL
个对象数组:
var urls = [NSURL(string: "file:///path/to/user/folder/2.PNG")!, NSURL(string: "file:///path/to/user/folder/100.PNG")!, NSURL(string: "file:///path/to/user/folder/101.PNG")!, NSURL(string: "file:///path/to/user/folder/1.PNG")! ]
我们可以使用 pathComponents
属性 来提取一个数组,其中包含 NSURL
路径中的所有组件(例如 ["/", "path", "to", "user", "folder", "2.PNG"]
)。
如果我们看到,我们可以按数组中的最后一个元素对文件进行排序,即删除扩展名和点 ("."
) 的文件名,在本例中为数字。让我们看看如何在下面的代码中做到这一点:
urls.sortInPlace {
// number of elements in each array
let c1 = [=11=].pathComponents!.count - 1
let c2 = .pathComponents!.count - 1
// the filename of each file
var v1 = [=11=].pathComponents![c1].componentsSeparatedByString(".")
var v2 = .pathComponents![c2].componentsSeparatedByString(".")
return Int(v1[0]) < Int(v2[0])
}
在上面的代码中,我们使用函数 sortInPlace
来避免创建另一个元素已排序的数组,但是如果您愿意,可以使用 sort
代替。代码中的另一个重点是return Int(v1[0]) < Int(v2[0])
行,在这一行中我们必须将字符串中的数字转换为实数,因为如果我们比较两个字符串"2"
和"100"
第二个小于大于,因为字符串是按字典序比较的。
所以数组 urls
应该像下面这样:
[file:///path/to/user/folder/1.PNG, file:///path/to/user/folder/2.PNG, file:///path/to/user/folder/100.PNG, file:///path/to/user/folder/101.PNG]
EDIT:
pathComponents
和componentsSeparatedByString
这两个函数增加了sortInPlace
算法的space复杂性,如果你能保证文件的路径总是相同,除了文件名应该是一个数字,您可以使用此代码代替:
urls.sortInPlace { [=13=].absoluteString.compare(
.absoluteString, options: .NumericSearch) == .OrderedAscending
}
希望对你有所帮助。
我正在将大量图像路径加载到 NSURL 中。
这些图像位于从 1.PNG、2.PNG、3.PNG 到 1500.PNG 排序的文件夹中。
当我尝试加载它们时:
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
print(url)
let fileManager = NSFileManager.defaultManager()
let properties = [NSURLLocalizedLabelKey,
NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
do {
imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.SkipsHiddenFiles)
} catch let error1 as NSError {
print(error1.description)
}
imageURLs 数组填充:
imageURLs[0] = ...[=11=].PNG
imageURLs[1] = ....PNG
imageURLs[2] = ...0.PNG
imageURLs[3] = ...00.PNG
而且不是数字顺序!
有人可以帮助对 imageURL 进行排序,或者当我在其上加载图像路径时或加载后?
因为你想按数字对文件进行排序,你必须首先解析实现它的路径,所以假设我们有以下 NSURL
个对象数组:
var urls = [NSURL(string: "file:///path/to/user/folder/2.PNG")!, NSURL(string: "file:///path/to/user/folder/100.PNG")!, NSURL(string: "file:///path/to/user/folder/101.PNG")!, NSURL(string: "file:///path/to/user/folder/1.PNG")! ]
我们可以使用 pathComponents
属性 来提取一个数组,其中包含 NSURL
路径中的所有组件(例如 ["/", "path", "to", "user", "folder", "2.PNG"]
)。
如果我们看到,我们可以按数组中的最后一个元素对文件进行排序,即删除扩展名和点 ("."
) 的文件名,在本例中为数字。让我们看看如何在下面的代码中做到这一点:
urls.sortInPlace {
// number of elements in each array
let c1 = [=11=].pathComponents!.count - 1
let c2 = .pathComponents!.count - 1
// the filename of each file
var v1 = [=11=].pathComponents![c1].componentsSeparatedByString(".")
var v2 = .pathComponents![c2].componentsSeparatedByString(".")
return Int(v1[0]) < Int(v2[0])
}
在上面的代码中,我们使用函数 sortInPlace
来避免创建另一个元素已排序的数组,但是如果您愿意,可以使用 sort
代替。代码中的另一个重点是return Int(v1[0]) < Int(v2[0])
行,在这一行中我们必须将字符串中的数字转换为实数,因为如果我们比较两个字符串"2"
和"100"
第二个小于大于,因为字符串是按字典序比较的。
所以数组 urls
应该像下面这样:
[file:///path/to/user/folder/1.PNG, file:///path/to/user/folder/2.PNG, file:///path/to/user/folder/100.PNG, file:///path/to/user/folder/101.PNG]
EDIT:
pathComponents
和componentsSeparatedByString
这两个函数增加了sortInPlace
算法的space复杂性,如果你能保证文件的路径总是相同,除了文件名应该是一个数字,您可以使用此代码代替:
urls.sortInPlace { [=13=].absoluteString.compare(
.absoluteString, options: .NumericSearch) == .OrderedAscending
}
希望对你有所帮助。