アプリのドキュメントフォルダに画像のリストがあります。作成した日付順に画像を読み込みたいです。どうやってやるの ?
1364 次
2 に答える
5
このコードは、ドキュメントディレクトリ内のすべてのファイルを作成された順序で列挙します。
何が起こっているのかを理解するには、コード内のコメントを参照してください。
NSFileManager *fm = [NSFileManager defaultManager];
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *err;
// Get all files with their creation date
NSArray *files = [fm contentsOfDirectoryAtURL:[[NSURL alloc] initFileURLWithPath:doc isDirectory:YES]
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLCreationDateKey]
options:0
error:&err];
// Using file's URL as the key, store creation date as the value
NSMutableDictionary *urlWithDate = [NSMutableDictionary dictionaryWithCapacity:files.count];
for (NSURL *f in files) {
NSDate *creationDate;
if ([f getResourceValue:&creationDate forKey:NSURLCreationDateKey error:&err]) {
[urlWithDate setObject:creationDate forKey:f];
}
}
// Sort the dictionary on the value, which is the creation key
for (NSURL *f in [urlWithDate keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}]) {
// Check if the file is an image. Load if it is an image, otherwise skip.
NSLog(@"%@", f);
}
于 2012-07-26T15:38:48.690 に答える
1
私は見てみましょう:グロブのあるディレクトリ内のファイルのリストを取得する
具体的にはNSFileManagerです。ファイルの属性を確認できます。そこから、NSPredicateを使用してソートを実行できる可能性があります。
于 2012-07-26T15:24:59.657 に答える