6

Ich versuche, eine einfache Fotoauswahl zu erstellen, die vorläufig zwei Optionen hat: Neueste und Favoriten. Was ich mache, ist, alle Fotos durch die creationDate zu bekommen, aber das gibt Bilder in der falschen Reihenfolge in meiner Datenquelle zurück. Am Anfang der Datenquelle befinden sich Fotos von vor Jahren, und überall sind weniger als ein paar Minuten alte Fotos verstreut. Ich denke, das Problem ist, dass ich das Haupt fetchResult die Sortierreihenfolge zuerst mitteilen muss, aber ich denke nicht, dass es möglich ist: Unsupported sort descriptor in fetch options: (creationDate, ascending, compare:PHFetchResult alle Fotos und sortieren nach Datum inkonsistent

Ich würde jede mögliche Hilfe schätzen schätzen. Code:

@property (nonatomic, strong) NSMutableOrderedSet *recentsDataSource; 
@property (nonatomic, strong) NSMutableOrderedSet *favoritesDataSource; 

- (void)setup 
{ 
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil]; 

    for (PHAssetCollection *sub in fetchResult) 
    { 
     PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init]; 

     fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

     PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions]; 

     for (PHAsset *asset in assetsInCollection) 
     { 
      [self.recentsDataSource addObject:asset]; 

      if (asset.isFavorite) 
      { 
       [self.favoritesDataSource addObject:asset]; 
      } 
     } 
    } 
} 

Antwort

5

ich das alleine, hier ist meine Lösung herausgefunden:

- (void)setup 
{ 
    self.recentsDataSource = [[NSMutableOrderedSet alloc]init]; 
    self.favoritesDataSource = [[NSMutableOrderedSet alloc]init]; 

    PHFetchResult *assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil]; 

    PHFetchResult *favoriteCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumFavorites options:nil]; 

    for (PHAssetCollection *sub in assetCollection) 
    { 
     PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil]; 

     for (PHAsset *asset in assetsInCollection) 
     { 
      [self.recentsDataSource addObject:asset]; 
     } 
    } 

    if (self.recentsDataSource.count > 0) 
    { 
     NSArray *array = [self.recentsDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]]; 

     self.recentsDataSource = [[NSMutableOrderedSet alloc]initWithArray:array]; 
    } 

    for (PHAssetCollection *sub in favoriteCollection) 
    { 
     PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil]; 

     for (PHAsset *asset in assetsInCollection) 
     { 
      [self.favoritesDataSource addObject:asset]; 
     } 
    } 

    if (self.favoritesDataSource.count > 0) 
    { 
     NSArray *array = [self.favoritesDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]]; 

     self.favoritesDataSource = [[NSMutableOrderedSet alloc]initWithArray:array]; 
    } 
} 
+0

PHFetchResult entspricht nicht den Protokollablauf, daher können wir es nicht in Gebrauch für .. in der Aussage. – saiday