Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
dz_TZImagePickerController
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shuijing
dz_TZImagePickerController
Commits
f934d979
Commit
f934d979
authored
Jul 16, 2020
by
yuzheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
监听相册变化的通知,及时刷新界面
parent
805b8d8c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
26 deletions
+62
-26
TZImagePickerController/TZImagePickerController/TZAssetModel.h
...gePickerController/TZImagePickerController/TZAssetModel.h
+4
-0
TZImagePickerController/TZImagePickerController/TZAssetModel.m
...gePickerController/TZImagePickerController/TZAssetModel.m
+5
-0
TZImagePickerController/TZImagePickerController/TZImageManager.m
...PickerController/TZImagePickerController/TZImageManager.m
+7
-5
TZImagePickerController/TZImagePickerController/TZImagePickerController.m
...troller/TZImagePickerController/TZImagePickerController.m
+11
-1
TZImagePickerController/TZImagePickerController/TZPhotoPickerController.m
...troller/TZImagePickerController/TZPhotoPickerController.m
+35
-20
No files found.
TZImagePickerController/TZImagePickerController/TZAssetModel.h
View file @
f934d979
...
...
@@ -8,6 +8,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
typedef
enum
:
NSUInteger
{
TZAssetModelMediaTypePhoto
=
0
,
...
...
@@ -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
;
...
...
@@ -48,5 +51,6 @@ typedef enum : NSUInteger {
@property
(
nonatomic
,
assign
)
BOOL
isCameraRoll
;
-
(
void
)
setResult
:(
PHFetchResult
*
)
result
needFetchAssets
:(
BOOL
)
needFetchAssets
;
-
(
void
)
refreshFetchResult
;
@end
TZImagePickerController/TZImagePickerController/TZAssetModel.m
View file @
f934d979
...
...
@@ -43,6 +43,11 @@
}
}
-
(
void
)
refreshFetchResult
{
PHFetchResult
*
fetchResult
=
[
PHAsset
fetchAssetsInAssetCollection
:
self
.
collection
options
:
self
.
options
];
[
self
setResult
:
fetchResult
];
}
-
(
void
)
setSelectedModels
:(
NSArray
*
)
selectedModels
{
_selectedModels
=
selectedModels
;
if
(
_models
)
{
...
...
TZImagePickerController/TZImagePickerController/TZImageManager.m
View file @
f934d979
...
...
@@ -117,7 +117,7 @@ static dispatch_once_t onceToken;
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
;
}
...
...
@@ -159,9 +159,9 @@ static dispatch_once_t onceToken;
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
]];
}
}
}
...
...
@@ -727,10 +727,12 @@ static dispatch_once_t onceToken;
#pragma mark - Private Method
-
(
TZAlbumModel
*
)
modelWithResult
:(
PHFetchResult
*
)
result
name
:(
NSString
*
)
name
isCameraRoll
:(
BOOL
)
isCameraRoll
needFetchAssets
:(
BOOL
)
needFetchAsset
s
{
-
(
TZAlbumModel
*
)
modelWithResult
:(
PHFetchResult
*
)
result
collection
:(
PHAssetCollection
*
)
collection
isCameraRoll
:(
BOOL
)
isCameraRoll
needFetchAssets
:(
BOOL
)
needFetchAssets
options
:(
PHFetchOptions
*
)
option
s
{
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
;
...
...
TZImagePickerController/TZImagePickerController/TZImagePickerController.m
View file @
f934d979
...
...
@@ -707,7 +707,7 @@
@end
@interface
TZAlbumPickerController
()
<
UITableViewDataSource
,
UITableViewDelegate
>
{
@interface
TZAlbumPickerController
()
<
UITableViewDataSource
,
UITableViewDelegate
,
PHPhotoLibraryChangeObserver
>
{
UITableView
*
_tableView
;
}
@property
(
nonatomic
,
strong
)
NSMutableArray
*
albumArr
;
...
...
@@ -717,6 +717,7 @@
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
];
[[
PHPhotoLibrary
sharedPhotoLibrary
]
registerChangeObserver
:
self
];
self
.
isFirstAppear
=
YES
;
self
.
view
.
backgroundColor
=
[
UIColor
whiteColor
];
...
...
@@ -786,6 +787,7 @@
}
-
(
void
)
dealloc
{
[[
PHPhotoLibrary
sharedPhotoLibrary
]
unregisterChangeObserver
:
self
];
// NSLog(@"%@ dealloc",NSStringFromClass(self.class));
}
...
...
@@ -797,6 +799,14 @@
return
[
super
preferredStatusBarStyle
];
}
#pragma mark - PHPhotoLibraryChangeObserver
-
(
void
)
photoLibraryDidChange
:(
PHChange
*
)
changeInstance
{
dispatch_async
(
dispatch_get_main_queue
(),
^
{
[
self
configTableView
];
});
}
#pragma mark - Layout
-
(
void
)
viewDidLayoutSubviews
{
...
...
TZImagePickerController/TZImagePickerController/TZPhotoPickerController.m
View file @
f934d979
...
...
@@ -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
;
...
...
@@ -76,6 +76,7 @@ static CGFloat itemMargin = 5;
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
];
[[
PHPhotoLibrary
sharedPhotoLibrary
]
registerChangeObserver
:
self
];
self
.
isFirstAppear
=
YES
;
TZImagePickerController
*
tzImagePickerVc
=
(
TZImagePickerController
*
)
self
.
navigationController
;
_isSelectOriginalPhoto
=
tzImagePickerVc
.
isSelectOriginalPhoto
;
...
...
@@ -116,16 +117,14 @@ static CGFloat itemMargin = 5;
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
];
}
});
}
...
...
@@ -163,13 +162,18 @@ static CGFloat itemMargin = 5;
}
-
(
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
);
...
...
@@ -183,11 +187,11 @@ static CGFloat itemMargin = 5;
_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
{
...
...
@@ -207,6 +211,7 @@ static CGFloat itemMargin = 5;
-
(
void
)
viewDidAppear
:(
BOOL
)
animated
{
[
super
viewDidAppear
:
animated
];
self
.
isFirstAppear
=
NO
;
// [self updateCachedAssets];
}
...
...
@@ -898,10 +903,20 @@ static CGFloat itemMargin = 5;
}
-
(
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment