2016-08-10 21 views
0

カメラのロールからすべての画像と動画を表示するギャラリーアプリがあるとします。 スクリーンショットを撮ると、カメラロールが変更されるため、アプリはうまく機能しなくなります。 クラッシュする理由は、photoLibraryDidChangeです。
私がプログラムを終了すると、写真を撮ってもう一度開いてみるとすべてうまくいきますが、スクリーンショットを撮るとプログラムはこの機能を1回だけでなく数回入力します。 どうすれば解決できますか?PHPhotoLibraryChangeObserverメソッドが頻繁に実行される理由は何ですか?

答えて

0

PhotoLibraryの変更オブザーバを追加し、変更を受け取るために関連する機能を実装する必要があります。例えば :その後

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

    [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self]; 
} 

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

    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self]; 
} 

、プロトコルPHPhotoLibraryChangeObserverのdidChange方法を上書きします。

#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]; 
       } 
      } 
     } 
    }); 
} 
関連する問題