Skip to content

Commit f934d97

Browse files
committed
监听相册变化的通知,及时刷新界面
1 parent 805b8d8 commit f934d97

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

TZImagePickerController/TZImagePickerController/TZAssetModel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010
#import <UIKit/UIKit.h>
11+
#import <Photos/Photos.h>
1112

1213
typedef enum : NSUInteger {
1314
TZAssetModelMediaTypePhoto = 0,
@@ -40,6 +41,8 @@ typedef enum : NSUInteger {
4041
@property (nonatomic, strong) NSString *name; ///< The album name
4142
@property (nonatomic, assign) NSInteger count; ///< Count of photos the album contain
4243
@property (nonatomic, strong) PHFetchResult *result;
44+
@property (nonatomic, strong) PHAssetCollection *collection;
45+
@property (nonatomic, strong) PHFetchOptions *options;
4346

4447
@property (nonatomic, strong) NSArray *models;
4548
@property (nonatomic, strong) NSArray *selectedModels;
@@ -48,5 +51,6 @@ typedef enum : NSUInteger {
4851
@property (nonatomic, assign) BOOL isCameraRoll;
4952

5053
- (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets;
54+
- (void)refreshFetchResult;
5155

5256
@end

TZImagePickerController/TZImagePickerController/TZAssetModel.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ - (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets
4343
}
4444
}
4545

46+
- (void)refreshFetchResult {
47+
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.collection options:self.options];
48+
[self setResult:fetchResult];
49+
}
50+
4651
- (void)setSelectedModels:(NSArray *)selectedModels {
4752
_selectedModels = selectedModels;
4853
if (_models) {

TZImagePickerController/TZImagePickerController/TZImageManager.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ - (void)getCameraRollAlbum:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allow
117117
if (collection.estimatedAssetCount <= 0) continue;
118118
if ([self isCameraRollAlbum:collection]) {
119119
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
120-
model = [self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:YES needFetchAssets:needFetchAssets];
120+
model = [self modelWithResult:fetchResult collection:collection isCameraRoll:YES needFetchAssets:needFetchAssets options:option];
121121
if (completion) completion(model);
122122
break;
123123
}
@@ -159,9 +159,9 @@ - (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickin
159159
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) continue;
160160
if (collection.assetCollectionSubtype == 1000000201) continue; //『最近删除』相册
161161
if ([self isCameraRollAlbum:collection]) {
162-
[albumArr insertObject:[self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:YES needFetchAssets:needFetchAssets] atIndex:0];
162+
[albumArr insertObject:[self modelWithResult:fetchResult collection:collection isCameraRoll:YES needFetchAssets:needFetchAssets options:option] atIndex:0];
163163
} else {
164-
[albumArr addObject:[self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:NO needFetchAssets:needFetchAssets]];
164+
[albumArr addObject:[self modelWithResult:fetchResult collection:collection isCameraRoll:NO needFetchAssets:needFetchAssets options:option]];
165165
}
166166
}
167167
}
@@ -727,10 +727,12 @@ - (BOOL)isPhotoSelectableWithAsset:(PHAsset *)asset {
727727

728728
#pragma mark - Private Method
729729

730-
- (TZAlbumModel *)modelWithResult:(PHFetchResult *)result name:(NSString *)name isCameraRoll:(BOOL)isCameraRoll needFetchAssets:(BOOL)needFetchAssets {
730+
- (TZAlbumModel *)modelWithResult:(PHFetchResult *)result collection:(PHAssetCollection *)collection isCameraRoll:(BOOL)isCameraRoll needFetchAssets:(BOOL)needFetchAssets options:(PHFetchOptions *)options {
731731
TZAlbumModel *model = [[TZAlbumModel alloc] init];
732732
[model setResult:result needFetchAssets:needFetchAssets];
733-
model.name = name;
733+
model.name = collection.localizedTitle;
734+
model.collection = collection;
735+
model.options = options;
734736
model.isCameraRoll = isCameraRoll;
735737
model.count = result.count;
736738
return model;

TZImagePickerController/TZImagePickerController/TZImagePickerController.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ - (void)callDelegateMethod {
707707
@end
708708

709709

710-
@interface TZAlbumPickerController ()<UITableViewDataSource,UITableViewDelegate> {
710+
@interface TZAlbumPickerController ()<UITableViewDataSource, UITableViewDelegate, PHPhotoLibraryChangeObserver> {
711711
UITableView *_tableView;
712712
}
713713
@property (nonatomic, strong) NSMutableArray *albumArr;
@@ -717,6 +717,7 @@ @implementation TZAlbumPickerController
717717

718718
- (void)viewDidLoad {
719719
[super viewDidLoad];
720+
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
720721
self.isFirstAppear = YES;
721722
self.view.backgroundColor = [UIColor whiteColor];
722723

@@ -786,6 +787,7 @@ - (void)configTableView {
786787
}
787788

788789
- (void)dealloc {
790+
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
789791
// NSLog(@"%@ dealloc",NSStringFromClass(self.class));
790792
}
791793

@@ -797,6 +799,14 @@ - (UIStatusBarStyle)preferredStatusBarStyle {
797799
return [super preferredStatusBarStyle];
798800
}
799801

802+
#pragma mark - PHPhotoLibraryChangeObserver
803+
804+
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
805+
dispatch_async(dispatch_get_main_queue(), ^{
806+
[self configTableView];
807+
});
808+
}
809+
800810
#pragma mark - Layout
801811

802812
- (void)viewDidLayoutSubviews {

TZImagePickerController/TZImagePickerController/TZPhotoPickerController.m

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
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

Comments
 (0)