PHPhotoLibraryChangeObserver方法执行得太频繁了,为什么?

时间:2016-08-10 11:18:46

标签: ios photosframework

当有一个图库应用程序时,我会在相机胶卷中显示所有图像和视频。 如果我截取屏幕截图,应用程序将无法正常工作,因为相机胶卷会发生变化。 崩溃的原因是photoLibraryDidChange功能 如果我退出程序拍照并重新打开它一切正常但是当我拍摄截图时我的程序进入这个功能几次而不是一次。 我该如何解决?

1 个答案:

答案 0 :(得分: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];
                }
            }
        }
    });
}
相关问题