2016-08-10 14 views
0

Wenn ich eine Galerie-App habe, in der ich alle Bilder und Videos von meiner Kamerarolle zeige. Wenn ich einen Screenshot mache, funktioniert die App nicht mehr gut, weil sich die Kamera verändert. Der Grund, warum es abstürzt, ist die photoLibraryDidChange Funktion.
Wenn ich das Programm beenden ein Foto machen und es wieder öffnen alles funktioniert gut, aber wenn ich einen Screenshot nehme mein Programm tritt diese Funktion ein paar Mal und nicht nur einmal. Wie kann ich das lösen?PHPhotoLibraryChangeObserver-Methode wird zu oft ausgeführt, warum?

Antwort

0

Sie müssen Change Observer für PhotoLibrary hinzufügen und die entsprechende Funktion implementieren, um die Änderung zu erhalten. Zum Beispiel:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self]; 
} 

Dann die didChange Methode des Protokolls PHPhotoLibraryChangeObserver überschrieben.

#pragma mark - <PHPhotoLibraryChangeObserver> 

- (void)photoLibraryDidChange:(PHChange *)changeInstance 
{ 
    // Check if there are changes to the assets we are showing. 
    PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:_fetchResult]; 
    if (collectionChanges == nil) { 
     return; 
    } 

    // Get the new fetch result. 
    _fetchResult = [collectionChanges fetchResultAfterChanges]; 

    /* 
    Change notifications may be made on a background queue. Re-dispatch to the 
    main queue before acting on the change as we'll be updating the UI. 
    */ 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     if (!_isCollectionViewLoaded) { 
      return ; 
     } 

     UICollectionView *collectionView = _collectionView; 

     if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) { 
      // Reload the collection view if the incremental diffs are not available 
      [collectionView reloadData]; 

     } else { 
      /* 
      Tell the collection view to animate insertions and deletions if we 
      have incremental diffs. 
      */ 

      NSArray<NSIndexPath *> * removedPaths = [[collectionChanges removedIndexes] aapl_indexPathsFromIndexesWithSection:0]; 
      NSArray<NSIndexPath *> * insertedPaths = [[collectionChanges insertedIndexes] aapl_indexPathsFromIndexesWithSection:0]; 
      NSArray<NSIndexPath *> * changedPaths = [[collectionChanges changedIndexes] aapl_indexPathsFromIndexesWithSection:0]; 

      BOOL shouldReload = NO; 

      if ((changedPaths != nil) + (removedPaths != nil) + (insertedPaths!= nil) > 1) { 
       shouldReload = YES; 
      } 

      if (shouldReload) { 
       [collectionView reloadData]; 

      } else { 

       @try { 
        [collectionView performBatchUpdates:^{ 
         if ([removedPaths count] > 0) { 
          [collectionView deleteItemsAtIndexPaths:removedPaths]; 
         } 

         if ([insertedPaths count] > 0) { 
          [collectionView insertItemsAtIndexPaths:insertedPaths]; 
         } 

         if ([changedPaths count] > 0) { 
          [collectionView reloadItemsAtIndexPaths:changedPaths]; 
         } 
        } completion:^(BOOL finished) { 
         if (_fetchResult.count == 0) { 
          MTLog(@"There is no photo in this album yet!!!"); 
          [self.navigationController popViewControllerAnimated:YES]; 
         } 
        }]; 

       } 
       @catch (NSException *exception) { 
        [collectionView reloadData]; 
       } 
      } 
     } 
    }); 
} 
Verwandte Themen