iOS의 응용 프로그램 폴더에서 날짜별로 UI이미지를 정렬하는 방법 [중복]
문제
갤러리에서 날짜 수정 유형별로 이미지를 표시하는 방법입니다.다음을 사용하여 폴더에서 데이터를 가져올 수 있습니다.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
또한 수정된 날짜별로 uiimage를 비교하는 방법도 있습니다.
미리 감사드립니다....
해결책
가장 간단한 방법:
- (void)images
{
//---Get directory from app folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *originalImage = [[NSMutableArray alloc]init];
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
[originalImage addObject:image];
}
NSLog(@"\n\n\n Original images %@",originalImage);
//---------sorting image by date modified
NSArray* filelist_date_sorted;
filelist_date_sorted = [imageFilenames sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDictionary* first_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj1] error:nil];
NSDate *first = [first_properties objectForKey:NSFileModificationDate];
NSDictionary *second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj2] error:nil];
NSDate *second = [second_properties objectForKey:NSFileModificationDate];
return [second compare:first];
}];
NSLog(@"\n\n\n date images %@",filelist_date_sorted);
NSMutableArray *sortedImage = [[NSMutableArray alloc]init];
//--- Store sorted images in array
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[filelist_date_sorted objectAtIndex:i] ];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
if(!(image==nil))
{
[sortedImage addObject:image];
}
}
NSLog(@"\n\n\nsorted images %@",sortedImage);
}
다른 팁
이를 위해서는 다음을 사용해야합니다. ALAssets라이브러리 갤러리의 모든 이미지를 열거합니다.
여기에서 각 이미지의 속성을 가져옵니다.
- 이미지 파일 이름.
- 이미지 자산 URL
그리고 당신의 대답은 여기에 있습니다:
다음을 사용하여 자산 생성 날짜를 검색할 수 있습니다. ALAsset속성 날짜 재산:
ALAsset* asset;
//do anthing
NSDate* date = [asset valueForProperty:ALAssetPropertyDate];
아마도 그것이 당신에게 도움이 될 것입니다.
이 코드를 사용하여 이미지의 속성을 얻을 수 있습니다
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:@"path/to/my/file" error:nil];
이 코드를 사용하여 수정된 날짜를 얻을 수 있습니다
NSDate *date = [attributes fileModificationDate];
NSMutableDictionary를 생성하고 수정된 날짜를 키로, 경로를 값으로 입력합니다.
NSMutableDictionary *imagesDict = [[NSMutableDictionary alloc] init];
for(NSString *path in paths)
{
NSDictionary *attributes = [[NSFileManager defaultManager] ;
NSDate *date = [attributes fileModificationDate];
imagesDict[date] = path; // key - date , value - path
}
그런 다음 ImagesDict의 키를 정렬할 수 있습니다.
NSArray *sortedDates = [[imagesDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
그런 다음 sortedDates 항목에 대한 주기를 만들고 이미지의 날짜 경로를 기준으로 정렬할 수 있습니다.
for(NSDate *newDate in sortedDates)
{
NSString newPath = imagesDict[newDate];
}
또 다른 해결책:
-(NSArray *)orderedFileListByModificationDate{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [paths objectAtIndex:0];
dirPath=[dirPath stringByAppendingString:@"/imgs"];
NSFileManager *fileManager=[[NSFileManager alloc] init];
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
int filesCount;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:NULL];
int dirFilesCount=(int)[directoryContent count];
NSMutableArray *fileObjects=[NSMutableArray array];
for (filesCount = 0; filesCount < dirFilesCount; filesCount++)
{
NSString *fileName=[directoryContent objectAtIndex:filesCount];
NSString *filePath =[NSString stringWithFormat:@"%@/%@",dirPath,fileName];
NSDictionary *fileAttributes=[fileManager attributesOfItemAtPath:filePath error:nil];
NSDate *dateModiftication=[fileAttributes valueForKey:@"NSFileModificationDate"];
NSMutableDictionary *datesAndFilesDict=[[NSMutableDictionary alloc] initWithCapacity:2];
[datesAndFilesDict setValue:fileName forKey:@"fileName"];
[datesAndFilesDict setValue:dateModiftication forKey:@"date"];
[fileObjects addObject:datesAndFilesDict];
}
NSLog(@"%@",fileObjects);
NSArray *newOrderedFileList = [fileObjects sortedArrayUsingComparator:
^(id obj1, id obj2) {
NSDate *dateOne=[(NSDictionary*)obj1 valueForKey:@"date"];
NSDate *dateTwo=[(NSDictionary*)obj2 valueForKey:@"date"];
NSComparisonResult result=[dateOne compare:dateTwo];
return result;
}];
NSLog(@"%@",newOrderedFileList);
//Now you've an array of dictionaries where you can pick the filename with the "fileName" key.
return newOrderedFileList;
}
제휴하지 않습니다 StackOverflow