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?
0
A
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
- 1. Wie oft wird der Steuerungsdefinitionsblock/Hauptkörper ausgeführt?
- 2. Warum wird Listbox.Drawitem oft aufgerufen?
- 3. Wie oft wird diese Schleife ausgeführt?
- 4. Swift: Warum wird die CustomStringConvertible-Beschreibung in diesem Fall zu oft ausgeführt?
- 5. Warum wird "Autoreconf" nicht oft verwendet?
- 6. Was wäre eine bessere DELETE-Abfrage, die oft ausgeführt wird?
- 7. Wie kann ich verfolgen, wie oft mein Skript ausgeführt wird?
- 8. JAVA Wie oft wird ein statischer Block ausgeführt?
- 9. Wie kann ich überprüfen, wie oft ein Replikationsagent ausgeführt wird?
- 10. Warum wiederholt sich die Iteration zu oft?
- 11. Fragmentierung eines bestimmten Index wird zu oft
- 12. Warum wird DispatchQueue.main.async nicht ausgeführt?
- 13. Warum wird das nicht ausgeführt, wenn der Block ausgeführt wird?
- 14. Warum wird CTE zweimal ausgeführt?
- 15. Warum wird Sonde nicht ausgeführt?
- 16. Warum wird HttpWebRequest.BeginGetResponse() synchron ausgeführt?
- 17. Warum wird hier sofort ausgeführt?
- 18. Wie oft wird onunload ausgelöst?
- 19. Android Baseadaper. getView wird zu oft aufgerufen
- 20. Wie oft wird dieses Codeteil unendlich oft gedruckt?
- 21. Wie oft wurde das Programm ausgeführt? C#
- 22. Wie oft sollten Oracle-Datenbankstatistiken ausgeführt werden?
- 23. Warum wird getCount im Adapter so oft aufgerufen?
- 24. Warum wird getCount so oft in PagerAdapter aufgerufen?
- 25. Warum läuft% timeit unterschiedlich oft?
- 26. Warum ist onPeersAvailable oft genannt
- 27. Warum sind Standards oft geschlossen?
- 28. Warum wird Java-Bytecode "speichern" oft von "laden" gefolgt?
- 29. Warum wird meine Controller-Funktion so oft aufgerufen?
- 30. Warum verwenden Generika oft T?