Skip to content

Commit

Permalink
监听相册变化的通知,及时刷新界面
Browse files Browse the repository at this point in the history
  • Loading branch information
banchichen committed Jul 16, 2020
1 parent 805b8d8 commit f934d97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>

typedef enum : NSUInteger {
TZAssetModelMediaTypePhoto = 0,
Expand Down Expand Up @@ -40,6 +41,8 @@ typedef enum : NSUInteger {
@property (nonatomic, strong) NSString *name; ///< The album name
@property (nonatomic, assign) NSInteger count; ///< Count of photos the album contain
@property (nonatomic, strong) PHFetchResult *result;
@property (nonatomic, strong) PHAssetCollection *collection;
@property (nonatomic, strong) PHFetchOptions *options;

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

- (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets;
- (void)refreshFetchResult;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ - (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets
}
}

- (void)refreshFetchResult {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.collection options:self.options];
[self setResult:fetchResult];
}

- (void)setSelectedModels:(NSArray *)selectedModels {
_selectedModels = selectedModels;
if (_models) {
Expand Down
12 changes: 7 additions & 5 deletions TZImagePickerController/TZImagePickerController/TZImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ - (void)getCameraRollAlbum:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allow
if (collection.estimatedAssetCount <= 0) continue;
if ([self isCameraRollAlbum:collection]) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
model = [self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:YES needFetchAssets:needFetchAssets];
model = [self modelWithResult:fetchResult collection:collection isCameraRoll:YES needFetchAssets:needFetchAssets options:option];
if (completion) completion(model);
break;
}
Expand Down Expand Up @@ -159,9 +159,9 @@ - (void)getAllAlbums:(BOOL)allowPickingVideo allowPickingImage:(BOOL)allowPickin
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) continue;
if (collection.assetCollectionSubtype == 1000000201) continue; //『最近删除』相册
if ([self isCameraRollAlbum:collection]) {
[albumArr insertObject:[self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:YES needFetchAssets:needFetchAssets] atIndex:0];
[albumArr insertObject:[self modelWithResult:fetchResult collection:collection isCameraRoll:YES needFetchAssets:needFetchAssets options:option] atIndex:0];
} else {
[albumArr addObject:[self modelWithResult:fetchResult name:collection.localizedTitle isCameraRoll:NO needFetchAssets:needFetchAssets]];
[albumArr addObject:[self modelWithResult:fetchResult collection:collection isCameraRoll:NO needFetchAssets:needFetchAssets options:option]];
}
}
}
Expand Down Expand Up @@ -727,10 +727,12 @@ - (BOOL)isPhotoSelectableWithAsset:(PHAsset *)asset {

#pragma mark - Private Method

- (TZAlbumModel *)modelWithResult:(PHFetchResult *)result name:(NSString *)name isCameraRoll:(BOOL)isCameraRoll needFetchAssets:(BOOL)needFetchAssets {
- (TZAlbumModel *)modelWithResult:(PHFetchResult *)result collection:(PHAssetCollection *)collection isCameraRoll:(BOOL)isCameraRoll needFetchAssets:(BOOL)needFetchAssets options:(PHFetchOptions *)options {
TZAlbumModel *model = [[TZAlbumModel alloc] init];
[model setResult:result needFetchAssets:needFetchAssets];
model.name = name;
model.name = collection.localizedTitle;
model.collection = collection;
model.options = options;
model.isCameraRoll = isCameraRoll;
model.count = result.count;
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ - (void)callDelegateMethod {
@end


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

- (void)viewDidLoad {
[super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
self.isFirstAppear = YES;
self.view.backgroundColor = [UIColor whiteColor];

Expand Down Expand Up @@ -786,6 +787,7 @@ - (void)configTableView {
}

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

Expand All @@ -797,6 +799,14 @@ - (UIStatusBarStyle)preferredStatusBarStyle {
return [super preferredStatusBarStyle];
}

#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
[self configTableView];
});
}

#pragma mark - Layout

- (void)viewDidLayoutSubviews {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#import <MobileCoreServices/MobileCoreServices.h>
#import "TZImageRequestOperation.h"

@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate, PHPhotoLibraryChangeObserver> {
NSMutableArray *_models;

UIView *_bottomToolBar;
Expand Down Expand Up @@ -76,6 +76,7 @@ - (UIImagePickerController *)imagePickerVc {

- (void)viewDidLoad {
[super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
self.isFirstAppear = YES;
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
_isSelectOriginalPhoto = tzImagePickerVc.isSelectOriginalPhoto;
Expand Down Expand Up @@ -116,16 +117,14 @@ - (void)fetchAssetModels {
self->_models = [NSMutableArray arrayWithArray:self->_model.models];
[self initSubviews];
}];
} else {
if (self->_showTakePhotoBtn || self->_isFirstAppear) {
[[TZImageManager manager] getAssetsFromFetchResult:self->_model.result completion:^(NSArray<TZAssetModel *> *models) {
self->_models = [NSMutableArray arrayWithArray:models];
[self initSubviews];
}];
} else {
self->_models = [NSMutableArray arrayWithArray:self->_model.models];
} else if (self->_showTakePhotoBtn || self->_isFirstAppear || !self.model.models) {
[[TZImageManager manager] getAssetsFromFetchResult:self->_model.result completion:^(NSArray<TZAssetModel *> *models) {
self->_models = [NSMutableArray arrayWithArray:models];
[self initSubviews];
}
}];
} else {
self->_models = [NSMutableArray arrayWithArray:self->_model.models];
[self initSubviews];
}
});
}
Expand Down Expand Up @@ -163,13 +162,18 @@ - (UIStatusBarStyle)preferredStatusBarStyle {
}

- (void)configCollectionView {
_layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[TZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.alwaysBounceHorizontal = NO;
_collectionView.contentInset = UIEdgeInsetsMake(itemMargin, itemMargin, itemMargin, itemMargin);
if (!_collectionView) {
_layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[TZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.alwaysBounceHorizontal = NO;
_collectionView.contentInset = UIEdgeInsetsMake(itemMargin, itemMargin, itemMargin, itemMargin);
[self.view addSubview:_collectionView];
[_collectionView registerClass:[TZAssetCell class] forCellWithReuseIdentifier:@"TZAssetCell"];
[_collectionView registerClass:[TZAssetCameraCell class] forCellWithReuseIdentifier:@"TZAssetCameraCell"];
}

if (_showTakePhotoBtn) {
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + self.columnNumber) / self.columnNumber) * self.view.tz_width);
Expand All @@ -183,11 +187,11 @@ - (void)configCollectionView {
_noDataLabel.textColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
_noDataLabel.font = [UIFont boldSystemFontOfSize:20];
[_collectionView addSubview:_noDataLabel];
} else if (_noDataLabel) {
[_noDataLabel removeFromSuperview];
_noDataLabel = nil;
}
}
[self.view addSubview:_collectionView];
[_collectionView registerClass:[TZAssetCell class] forCellWithReuseIdentifier:@"TZAssetCell"];
[_collectionView registerClass:[TZAssetCameraCell class] forCellWithReuseIdentifier:@"TZAssetCameraCell"];
}

- (void)viewWillAppear:(BOOL)animated {
Expand All @@ -207,6 +211,7 @@ - (void)viewWillAppear:(BOOL)animated {

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.isFirstAppear = NO;
// [self updateCachedAssets];
}

Expand Down Expand Up @@ -898,10 +903,20 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
}

- (void)dealloc {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// NSLog(@"%@ dealloc",NSStringFromClass(self.class));
}

#pragma mark - PHPhotoLibraryChangeObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
[self.model refreshFetchResult];
[self fetchAssetModels];
});
}

#pragma mark - Asset Caching

- (void)resetCachedAssets {
Expand Down

0 comments on commit f934d97

Please sign in to comment.