1919#import < MobileCoreServices/MobileCoreServices.h>
2020#import " TZImageRequestOperation.h"
2121
22- @interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
22+ @interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate, PHPhotoLibraryChangeObserver > {
2323 NSMutableArray *_models;
2424
2525 UIView *_bottomToolBar;
@@ -76,6 +76,7 @@ - (UIImagePickerController *)imagePickerVc {
7676
7777- (void )viewDidLoad {
7878 [super viewDidLoad ];
79+ [[PHPhotoLibrary sharedPhotoLibrary ] registerChangeObserver: self ];
7980 self.isFirstAppear = YES ;
8081 TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController ;
8182 _isSelectOriginalPhoto = tzImagePickerVc.isSelectOriginalPhoto ;
@@ -116,16 +117,14 @@ - (void)fetchAssetModels {
116117 self->_models = [NSMutableArray arrayWithArray: self ->_model.models];
117118 [self initSubviews ];
118119 }];
119- } else {
120- if (self->_showTakePhotoBtn || self->_isFirstAppear ) {
121- [[TZImageManager manager ] getAssetsFromFetchResult: self ->_model.result completion: ^(NSArray <TZAssetModel *> *models) {
122- self->_models = [NSMutableArray arrayWithArray: models];
123- [self initSubviews ];
124- }];
125- } else {
126- self->_models = [NSMutableArray arrayWithArray: self ->_model.models];
120+ } else if (self->_showTakePhotoBtn || self->_isFirstAppear || !self.model .models ) {
121+ [[TZImageManager manager ] getAssetsFromFetchResult: self ->_model.result completion: ^(NSArray <TZAssetModel *> *models) {
122+ self->_models = [NSMutableArray arrayWithArray: models];
127123 [self initSubviews ];
128- }
124+ }];
125+ } else {
126+ self->_models = [NSMutableArray arrayWithArray: self ->_model.models];
127+ [self initSubviews ];
129128 }
130129 });
131130}
@@ -163,13 +162,18 @@ - (UIStatusBarStyle)preferredStatusBarStyle {
163162}
164163
165164- (void )configCollectionView {
166- _layout = [[UICollectionViewFlowLayout alloc ] init ];
167- _collectionView = [[TZCollectionView alloc ] initWithFrame: CGRectZero collectionViewLayout: _layout];
168- _collectionView.backgroundColor = [UIColor whiteColor ];
169- _collectionView.dataSource = self;
170- _collectionView.delegate = self;
171- _collectionView.alwaysBounceHorizontal = NO ;
172- _collectionView.contentInset = UIEdgeInsetsMake (itemMargin, itemMargin, itemMargin, itemMargin);
165+ if (!_collectionView) {
166+ _layout = [[UICollectionViewFlowLayout alloc ] init ];
167+ _collectionView = [[TZCollectionView alloc ] initWithFrame: CGRectZero collectionViewLayout: _layout];
168+ _collectionView.backgroundColor = [UIColor whiteColor ];
169+ _collectionView.dataSource = self;
170+ _collectionView.delegate = self;
171+ _collectionView.alwaysBounceHorizontal = NO ;
172+ _collectionView.contentInset = UIEdgeInsetsMake (itemMargin, itemMargin, itemMargin, itemMargin);
173+ [self .view addSubview: _collectionView];
174+ [_collectionView registerClass: [TZAssetCell class ] forCellWithReuseIdentifier: @" TZAssetCell" ];
175+ [_collectionView registerClass: [TZAssetCameraCell class ] forCellWithReuseIdentifier: @" TZAssetCameraCell" ];
176+ }
173177
174178 if (_showTakePhotoBtn) {
175179 _collectionView.contentSize = CGSizeMake (self.view .tz_width , ((_model.count + self.columnNumber ) / self.columnNumber ) * self.view .tz_width );
@@ -183,11 +187,11 @@ - (void)configCollectionView {
183187 _noDataLabel.textColor = [UIColor colorWithRed: rgb green: rgb blue: rgb alpha: 1.0 ];
184188 _noDataLabel.font = [UIFont boldSystemFontOfSize: 20 ];
185189 [_collectionView addSubview: _noDataLabel];
190+ } else if (_noDataLabel) {
191+ [_noDataLabel removeFromSuperview ];
192+ _noDataLabel = nil ;
186193 }
187194 }
188- [self .view addSubview: _collectionView];
189- [_collectionView registerClass: [TZAssetCell class ] forCellWithReuseIdentifier: @" TZAssetCell" ];
190- [_collectionView registerClass: [TZAssetCameraCell class ] forCellWithReuseIdentifier: @" TZAssetCameraCell" ];
191195}
192196
193197- (void )viewWillAppear : (BOOL )animated {
@@ -207,6 +211,7 @@ - (void)viewWillAppear:(BOOL)animated {
207211
208212- (void )viewDidAppear : (BOOL )animated {
209213 [super viewDidAppear: animated];
214+ self.isFirstAppear = NO ;
210215 // [self updateCachedAssets];
211216}
212217
@@ -898,10 +903,20 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
898903}
899904
900905- (void )dealloc {
906+ [[PHPhotoLibrary sharedPhotoLibrary ] unregisterChangeObserver: self ];
901907 [[NSNotificationCenter defaultCenter ] removeObserver: self ];
902908 // NSLog(@"%@ dealloc",NSStringFromClass(self.class));
903909}
904910
911+ #pragma mark - PHPhotoLibraryChangeObserver
912+
913+ - (void )photoLibraryDidChange : (PHChange *)changeInstance {
914+ dispatch_async (dispatch_get_main_queue (), ^{
915+ [self .model refreshFetchResult ];
916+ [self fetchAssetModels ];
917+ });
918+ }
919+
905920#pragma mark - Asset Caching
906921
907922- (void )resetCachedAssets {
0 commit comments