简体   繁体   English

从用户照片库加载图像并更新UICollectionView

[英]Load images from user photo library and update UICollectionView

So i have UICollectionView . 所以我有UICollectionView I need to get all user photos from his photos library and when photo is loaded update my collection view. 我需要从他的照片库中获取所有用户照片,并且在加载照片时更新我的​​收藏夹视图。 I tried using NSNotificationCenter . 我尝试使用NSNotificationCenter I added observer to UICollectionViewController and when i enumerate photos i post notification so i could update my collection view when photo us loaded. 我将观察者添加到UICollectionViewController ,当我枚举照片时,我会发布通知,以便在加载照片时可以更新收藏视图。 Here is my controller: 这是我的控制器:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(phonePhotoLoaded:) name:@"PhonePhotoLoaded" object:nil]; [self.manager getPhonePhotos]; 

Selector method: 选择器方法:

 -(void)phonePhotoLoaded:(NSNotification *)userInfo { [self.collectionView performBatchUpdates:^{ NSDictionary *dic = [userInfo valueForKey:@"userInfo"]; [self.photos addObject:[dic valueForKey:@"photo"]]; [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.photos.count inSection:0]]]; } completion:^(BOOL finished){ }]; 

} }

And in [self.manager getPhonePhotos] [self.manager getPhonePhotos]

 ALAssetsLibrary *al = [PhotoManager defaultAssetsLibrary]; [al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){ [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){ if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { NSDictionary *userPhotos = [NSDictionary dictionaryWithObjectsAndKeys:asset, @"photo", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"PhonePhotoLoaded" object:self userInfo:userPhotos]; } }]; }failureBlock:^(NSError *error) { ;} ]; 

This is what error i get: 这是我得到什么错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'too many update animations on one view - limit is 31 in flight at a time (; }; layer = >)' ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“一个视图上的更新动画太多-一次飞行限制为31个(;}; layer =>)”

So as much as i understand, when i enumerate photos, i post notification, then notification method is executed but UICollectionView is not making animations. 据我所知,当我枚举照片时,我会发布通知,然后执行通知方法,但UICollectionView不会制作动画。 He somehow starts them when i finish enumerating. 当我完成枚举时,他会以某种方式启动它们。 So how could i update my UI when photo is fetched? 那么,提取照片后如何更新用户界面? Any ideas? 有任何想法吗?

Don't post a notification on each iteration of the photo enumeration (each time your block is called). 不要在每次照片枚举迭代时发布通知(每次调用您的图块时)。 Instead, build up an array of all of the assets and post it on the last call of the block (IIRC, the asset will be nil on the last call, need to check the docs). 取而代之的是,构建一个包含所有资产的数组,并将其发布到该块的最后一次调用中(IIRC,该资产在最后一次调用中将为nil,需要检查文档)。

Your current problem is that you are starting lots of animations by posting a notification each time and you should really collate all of your changes and then execute a single performBatchUpdates . 当前的问题是,每次发布通知都会启动大量动画,因此您应该真正整理所有更改,然后执行一个performBatchUpdates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM