6

Sto provando a creare un semplice raccoglitore di foto che ha due opzioni per ora: Recenti e Preferiti. Quello che sto facendo è cercare di ottenere tutte le foto dal creationDate, tuttavia questo sta restituendo le immagini nell'ordine sbagliato nella mia fonte di dati. Ci sono foto di anni fa all'inizio della fonte dei dati e foto che hanno meno di qualche minuto prima. Penso che il problema sia che devo prima dire al principale fetchResult l'ordinamento, ma non penso sia possibile: Unsupported sort descriptor in fetch options: (creationDate, ascending, compare:PHFetchResult ottiene tutte le foto e ordina per data incoerente

Apprezzerei qualsiasi aiuto offerto. Codice:

@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]; 
      } 
     } 
    } 
} 

risposta

5

ho capito questo da solo, ecco la mia soluzione:

- (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 non è conforme alla sequenza di protocollo, quindi non siamo in grado di utilizzarlo all'interno per .. in dichiarazione. – saiday