Commit f934d979 authored by yuzheng's avatar yuzheng

监听相册变化的通知,及时刷新界面

parent 805b8d8c
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <Photos/Photos.h>
typedef enum : NSUInteger { typedef enum : NSUInteger {
TZAssetModelMediaTypePhoto = 0, TZAssetModelMediaTypePhoto = 0,
...@@ -40,6 +41,8 @@ typedef enum : NSUInteger { ...@@ -40,6 +41,8 @@ typedef enum : NSUInteger {
@property (nonatomic, strong) NSString *name; ///< The album name @property (nonatomic, strong) NSString *name; ///< The album name
@property (nonatomic, assign) NSInteger count; ///< Count of photos the album contain @property (nonatomic, assign) NSInteger count; ///< Count of photos the album contain
@property (nonatomic, strong) PHFetchResult *result; @property (nonatomic, strong) PHFetchResult *result;
@property (nonatomic, strong) PHAssetCollection *collection;
@property (nonatomic, strong) PHFetchOptions *options;
@property (nonatomic, strong) NSArray *models; @property (nonatomic, strong) NSArray *models;
@property (nonatomic, strong) NSArray *selectedModels; @property (nonatomic, strong) NSArray *selectedModels;
...@@ -48,5 +51,6 @@ typedef enum : NSUInteger { ...@@ -48,5 +51,6 @@ typedef enum : NSUInteger {
@property (nonatomic, assign) BOOL isCameraRoll; @property (nonatomic, assign) BOOL isCameraRoll;
- (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets; - (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets;
- (void)refreshFetchResult;
@end @end
...@@ -43,6 +43,11 @@ ...@@ -43,6 +43,11 @@
} }
} }
- (void)refreshFetchResult {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.collection options:self.options];
[self setResult:fetchResult];
}
- (void)setSelectedModels:(NSArray *)selectedModels { - (void)setSelectedModels:(NSArray *)selectedModels {
_selectedModels = selectedModels; _selectedModels = selectedModels;
if (_models) { if (_models) {
......
...@@ -117,7 +117,7 @@ static dispatch_once_t onceToken; ...@@ -117,7 +117,7 @@ static dispatch_once_t onceToken;
if (collection.estimatedAssetCount <= 0) continue; if (collection.estimatedAssetCount <= 0) continue;
if ([self isCameraRollAlbum:collection]) { if ([self isCameraRollAlbum:collection]) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option]; 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); if (completion) completion(model);
break; break;
} }
...@@ -159,9 +159,9 @@ static dispatch_once_t onceToken; ...@@ -159,9 +159,9 @@ static dispatch_once_t onceToken;
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) continue; if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) continue;
if (collection.assetCollectionSubtype == 1000000201) continue; //『最近删除』相册 if (collection.assetCollectionSubtype == 1000000201) continue; //『最近删除』相册
if ([self isCameraRollAlbum:collection]) { 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 { } 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]];
} }
} }
} }
...@@ -727,10 +727,12 @@ static dispatch_once_t onceToken; ...@@ -727,10 +727,12 @@ static dispatch_once_t onceToken;
#pragma mark - Private Method #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]; TZAlbumModel *model = [[TZAlbumModel alloc] init];
[model setResult:result needFetchAssets:needFetchAssets]; [model setResult:result needFetchAssets:needFetchAssets];
model.name = name; model.name = collection.localizedTitle;
model.collection = collection;
model.options = options;
model.isCameraRoll = isCameraRoll; model.isCameraRoll = isCameraRoll;
model.count = result.count; model.count = result.count;
return model; return model;
......
...@@ -707,7 +707,7 @@ ...@@ -707,7 +707,7 @@
@end @end
@interface TZAlbumPickerController ()<UITableViewDataSource,UITableViewDelegate> { @interface TZAlbumPickerController ()<UITableViewDataSource, UITableViewDelegate, PHPhotoLibraryChangeObserver> {
UITableView *_tableView; UITableView *_tableView;
} }
@property (nonatomic, strong) NSMutableArray *albumArr; @property (nonatomic, strong) NSMutableArray *albumArr;
...@@ -717,6 +717,7 @@ ...@@ -717,6 +717,7 @@
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
self.isFirstAppear = YES; self.isFirstAppear = YES;
self.view.backgroundColor = [UIColor whiteColor]; self.view.backgroundColor = [UIColor whiteColor];
...@@ -786,6 +787,7 @@ ...@@ -786,6 +787,7 @@
} }
- (void)dealloc { - (void)dealloc {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
// NSLog(@"%@ dealloc",NSStringFromClass(self.class)); // NSLog(@"%@ dealloc",NSStringFromClass(self.class));
} }
...@@ -797,6 +799,14 @@ ...@@ -797,6 +799,14 @@
return [super preferredStatusBarStyle]; return [super preferredStatusBarStyle];
} }
#pragma mark - PHPhotoLibraryChangeObserver
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
[self configTableView];
});
}
#pragma mark - Layout #pragma mark - Layout
- (void)viewDidLayoutSubviews { - (void)viewDidLayoutSubviews {
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#import <MobileCoreServices/MobileCoreServices.h> #import <MobileCoreServices/MobileCoreServices.h>
#import "TZImageRequestOperation.h" #import "TZImageRequestOperation.h"
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { @interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate, PHPhotoLibraryChangeObserver> {
NSMutableArray *_models; NSMutableArray *_models;
UIView *_bottomToolBar; UIView *_bottomToolBar;
...@@ -76,6 +76,7 @@ static CGFloat itemMargin = 5; ...@@ -76,6 +76,7 @@ static CGFloat itemMargin = 5;
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
self.isFirstAppear = YES; self.isFirstAppear = YES;
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
_isSelectOriginalPhoto = tzImagePickerVc.isSelectOriginalPhoto; _isSelectOriginalPhoto = tzImagePickerVc.isSelectOriginalPhoto;
...@@ -116,8 +117,7 @@ static CGFloat itemMargin = 5; ...@@ -116,8 +117,7 @@ static CGFloat itemMargin = 5;
self->_models = [NSMutableArray arrayWithArray:self->_model.models]; self->_models = [NSMutableArray arrayWithArray:self->_model.models];
[self initSubviews]; [self initSubviews];
}]; }];
} else { } else if (self->_showTakePhotoBtn || self->_isFirstAppear || !self.model.models) {
if (self->_showTakePhotoBtn || self->_isFirstAppear) {
[[TZImageManager manager] getAssetsFromFetchResult:self->_model.result completion:^(NSArray<TZAssetModel *> *models) { [[TZImageManager manager] getAssetsFromFetchResult:self->_model.result completion:^(NSArray<TZAssetModel *> *models) {
self->_models = [NSMutableArray arrayWithArray:models]; self->_models = [NSMutableArray arrayWithArray:models];
[self initSubviews]; [self initSubviews];
...@@ -126,7 +126,6 @@ static CGFloat itemMargin = 5; ...@@ -126,7 +126,6 @@ static CGFloat itemMargin = 5;
self->_models = [NSMutableArray arrayWithArray:self->_model.models]; self->_models = [NSMutableArray arrayWithArray:self->_model.models];
[self initSubviews]; [self initSubviews];
} }
}
}); });
} }
...@@ -163,6 +162,7 @@ static CGFloat itemMargin = 5; ...@@ -163,6 +162,7 @@ static CGFloat itemMargin = 5;
} }
- (void)configCollectionView { - (void)configCollectionView {
if (!_collectionView) {
_layout = [[UICollectionViewFlowLayout alloc] init]; _layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[TZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout]; _collectionView = [[TZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
_collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.backgroundColor = [UIColor whiteColor];
...@@ -170,6 +170,10 @@ static CGFloat itemMargin = 5; ...@@ -170,6 +170,10 @@ static CGFloat itemMargin = 5;
_collectionView.delegate = self; _collectionView.delegate = self;
_collectionView.alwaysBounceHorizontal = NO; _collectionView.alwaysBounceHorizontal = NO;
_collectionView.contentInset = UIEdgeInsetsMake(itemMargin, itemMargin, itemMargin, itemMargin); _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) { if (_showTakePhotoBtn) {
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + self.columnNumber) / self.columnNumber) * self.view.tz_width); _collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + self.columnNumber) / self.columnNumber) * self.view.tz_width);
...@@ -183,11 +187,11 @@ static CGFloat itemMargin = 5; ...@@ -183,11 +187,11 @@ static CGFloat itemMargin = 5;
_noDataLabel.textColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0]; _noDataLabel.textColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
_noDataLabel.font = [UIFont boldSystemFontOfSize:20]; _noDataLabel.font = [UIFont boldSystemFontOfSize:20];
[_collectionView addSubview:_noDataLabel]; [_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 { - (void)viewWillAppear:(BOOL)animated {
...@@ -207,6 +211,7 @@ static CGFloat itemMargin = 5; ...@@ -207,6 +211,7 @@ static CGFloat itemMargin = 5;
- (void)viewDidAppear:(BOOL)animated { - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; [super viewDidAppear:animated];
self.isFirstAppear = NO;
// [self updateCachedAssets]; // [self updateCachedAssets];
} }
...@@ -898,10 +903,20 @@ static CGFloat itemMargin = 5; ...@@ -898,10 +903,20 @@ static CGFloat itemMargin = 5;
} }
- (void)dealloc { - (void)dealloc {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
// NSLog(@"%@ dealloc",NSStringFromClass(self.class)); // 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 #pragma mark - Asset Caching
- (void)resetCachedAssets { - (void)resetCachedAssets {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment