2016-12-12 14 views
0

一番古い写真が一番上に表示されるように、私の結果を日にちで注文します。PHFetchResultカスタムソート:日付順、古い写真順

私は現在PHAsset fetchAssetsWithMediaTypeを使用して写真を取り出しています:

@property PHFetchResult *photos; 

self.photos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 

を理想的には私が最初の日で注文した結果、最も古い写真が欲しいです。このように:私はPHFetchOptionsが述語といくつかの並べ替え記述子(https://developer.apple.com/reference/photos/phfetchoptions)でオブジェクトを渡すことができます見てきました

[11 Dec] 
Photo #20 with creation time 17:00 
Photo #21 with creation time 18:30 
Photo #22 with creation time 19:00 
Photo #23 with creation time 20:40 
Photo #24 with creation time 21:00 
[10 Dec] 
Photo #16 with creation time 10:30 
Photo #17 with creation time 11:00 
Photo #18 with creation time 12:20 
Photo #19 with creation time 13:00 
[9 Dec] 
Photo #14 with creation time 16:30 
Photo #15 with creation time 17:00 

、あなたがそれらを指定する方法を私に提案することができます(私はのCreationDate属性を使用してそれらを並べ替える必要があると考えています)私は希望の順序を得ることができるように?

答えて

0

PHFetchOptionsを使用する必要があり、ここで書かれたとしてしかし、それは(https://developer.apple.com/reference/photos/phfetchoptions/1624771-sortdescriptors)(それは単にSQLクエリを行いますので、私は信じて)カスタム比較を無視し、sortDescriptorsを受け入れ:

写真をん のsortDescriptorWithKey:ascending:comparator:メソッドで作成されたソートデスクリプタをサポートしていません。

答えは、あなたが新しいNSArrayの中で結果を動かすべきであるということですし、それらを取得した後、それらを注文:

- (void)loadPhotos 
{ 
    PHFetchResult *photosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 

    self.photos = [@[] mutableCopy]; 
    for(PHAsset *asset in photosResult){ 
     [self.photos addObject:asset]; 
    } 
    [self.photos sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" 
                     ascending:YES 
                    comparator:^NSComparisonResult(NSDate *dateTime1, NSDate *dateTime2) { 
                     unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; 
                     NSCalendar* calendar = [NSCalendar currentCalendar]; 

                     NSDateComponents* components1 = [calendar components:flags fromDate:dateTime1]; 
                     NSDate* date1 = [calendar dateFromComponents:components1]; 

                     NSDateComponents* components2 = [calendar components:flags fromDate:dateTime2]; 
                     NSDate* date2 = [calendar dateFromComponents:components2]; 

                     NSComparisonResult comparedDates = [date1 compare:date2]; 
                     if(comparedDates == NSOrderedSame) 
                     { 
                      return [dateTime2 compare:dateTime1]; 
                     } 
                     return comparedDates; 
                    } 
             ]]]; 

} 

は、私は巨大なカメラロールにこのソリューションをテストしていませんでした(ここで並べ替えメモリで完了し、パフォーマンスのボトルネックになる可能性があります)が、これが助けてくれることを願っています。

3

あなたはPHFetchOptions

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
    fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:true]]; 
    PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; 
+0

ありがとうございますが、これは私が求めていたものではありません。結果は一日(結果的には作成日の降順)、最も古い写真が最初に(したがって作成時間は昇順) –

+0

OMG .. –

関連する問題