Commit a42e994c authored by banchichen's avatar banchichen

开放预览功能,可在外界打开预览界面

parent 6950bfde
......@@ -28,6 +28,8 @@
/// Use this init method / 用这个初始化方法
- (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate;
/// This init method just for previewing photos / 用这个初始化方法以预览图片
- (instancetype)initWithSelectedAssets:(NSMutableArray *)selectedAssets selectedPhotos:(NSMutableArray *)selectedPhotos index:(NSInteger)index;
/// Default is 9 / 默认最大可选9张图片
@property (nonatomic, assign) NSInteger maxImagesCount;
......@@ -53,7 +55,7 @@
/// The photos user have selected
/// 用户选中过的图片数组
@property (nonatomic, strong) NSArray *selectedAssets;
@property (nonatomic, strong) NSMutableArray *selectedAssets;
@property (nonatomic, strong) NSMutableArray<TZAssetModel *> *selectedModels;
- (void)showAlertWithTitle:(NSString *)title;
......
......@@ -108,6 +108,29 @@
return self;
}
/// This init method just for previewing photos / 用这个初始化方法以预览图片
- (instancetype)initWithSelectedAssets:(NSMutableArray *)selectedAssets selectedPhotos:(NSMutableArray *)selectedPhotos index:(NSInteger)index{
TZPhotoPreviewController *previewVc = [[TZPhotoPreviewController alloc] init];
self = [super initWithRootViewController:previewVc];
if (self) {
self.selectedAssets = selectedAssets;
self.allowPickingOriginalPhoto = YES;
self.timeout = 15;
self.photoWidth = 828.0;
self.maxImagesCount = selectedAssets.count;
previewVc.photos = [NSMutableArray arrayWithArray:selectedPhotos];
previewVc.currentIndex = index;
[previewVc setOkButtonClickBlockWithPreviewType:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (self.didFinishPickingPhotosHandle) {
self.didFinishPickingPhotosHandle(photos,assets,isSelectOriginalPhoto);
}
[self dismissViewControllerAnimated:YES completion:nil];
}];
}
return self;
}
- (void)observeAuthrizationStatusChange {
if ([[TZImageManager manager] authorizationStatusAuthorized]) {
[self pushToPhotoPickerVc];
......@@ -190,7 +213,7 @@
}
}
- (void)setSelectedAssets:(NSArray *)selectedAssets {
- (void)setSelectedAssets:(NSMutableArray *)selectedAssets {
_selectedAssets = selectedAssets;
_selectedModels = [NSMutableArray array];
for (id asset in selectedAssets) {
......
......@@ -16,7 +16,7 @@
#import "TZVideoPlayerController.h"
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
NSMutableArray *_photoArr;
NSMutableArray *_models;
UIButton *_previewButton;
UIButton *_okButton;
......@@ -68,11 +68,11 @@ static CGSize AssetGridThumbnailSize;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] || !iOS8Later) {
[[TZImageManager manager] getAssetsFromFetchResult:_model.result allowPickingVideo:tzImagePickerVc.allowPickingVideo allowPickingImage:tzImagePickerVc.allowPickingImage completion:^(NSArray<TZAssetModel *> *models) {
_photoArr = [NSMutableArray arrayWithArray:models];
_models = [NSMutableArray arrayWithArray:models];
[self initSubviews];
}];
} else {
_photoArr = [NSMutableArray arrayWithArray:_model.models];
_models = [NSMutableArray arrayWithArray:_model.models];
[self initSubviews];
}
// [self resetCachedAssets];
......@@ -228,9 +228,7 @@ static CGSize AssetGridThumbnailSize;
}
- (void)previewButtonClick {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
TZPhotoPreviewController *photoPreviewVc = [[TZPhotoPreviewController alloc] init];
photoPreviewVc.photoArr = [NSArray arrayWithArray:tzImagePickerVc.selectedModels];
[self pushPhotoPrevireViewController:photoPreviewVc];
}
......@@ -287,22 +285,22 @@ static CGSize AssetGridThumbnailSize;
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] ) {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.allowPickingImage) {
return _photoArr.count + 1;
return _models.count + 1;
}
}
return _photoArr.count;
return _models.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// the cell lead to take a picture / 去拍照的cell
if (indexPath.row >= _photoArr.count) {
if (indexPath.row >= _models.count) {
TZAssetCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCameraCell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamedFromMyBundle:@"takePicture.png"];
return cell;
}
// the cell dipaly photo or video / 展示照片或视频的cell
TZAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCell" forIndexPath:indexPath];
TZAssetModel *model = _photoArr[indexPath.row];
TZAssetModel *model = _models[indexPath.row];
cell.model = model;
__weak typeof(cell) weakCell = cell;
......@@ -339,11 +337,11 @@ static CGSize AssetGridThumbnailSize;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// take a picture / 去拍照
if (indexPath.row >= _photoArr.count) {
if (indexPath.row >= _models.count) {
[self takePicture]; return;
}
// preview phote or video / 预览照片或视频
TZAssetModel *model = _photoArr[indexPath.row];
TZAssetModel *model = _models[indexPath.row];
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (model.type == TZAssetModelMediaTypeVideo) {
if (tzImagePickerVc.selectedModels.count > 0) {
......@@ -356,8 +354,8 @@ static CGSize AssetGridThumbnailSize;
}
} else {
TZPhotoPreviewController *photoPreviewVc = [[TZPhotoPreviewController alloc] init];
photoPreviewVc.photoArr = _photoArr;
photoPreviewVc.currentIndex = indexPath.row;
photoPreviewVc.models = _models;
[self pushPhotoPrevireViewController:photoPreviewVc];
}
}
......@@ -426,6 +424,9 @@ static CGSize AssetGridThumbnailSize;
/// Scale image / 缩放图片
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {
if (image.size.width < size.width) {
return image;
}
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
......@@ -434,8 +435,8 @@ static CGSize AssetGridThumbnailSize;
}
- (void)scrollCollectionViewToBottom {
if (_shouldScrollToBottom && _photoArr.count > 0) {
NSInteger item = _photoArr.count - 1;
if (_shouldScrollToBottom && _models.count > 0) {
NSInteger item = _models.count - 1;
if ([_model.name isEqualToString:@"相机胶卷"] || [_model.name isEqualToString:@"Camera Roll"] || [_model.name isEqualToString:@"所有照片"] ) {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.allowPickingImage) {
......@@ -448,7 +449,7 @@ static CGSize AssetGridThumbnailSize;
}
- (void)checkSelectedModels {
for (TZAssetModel *model in _photoArr) {
for (TZAssetModel *model in _models) {
model.isSelected = NO;
NSMutableArray *selectedAssets = [NSMutableArray array];
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
......@@ -484,7 +485,7 @@ static CGSize AssetGridThumbnailSize;
[tzImagePickerVc hideProgressHUD];
TZAssetModel *model = [models lastObject];
[_photoArr addObject:model];
[_models addObject:model];
if (tzImagePickerVc.selectedModels.count < tzImagePickerVc.maxImagesCount) {
model.isSelected = YES;
[tzImagePickerVc.selectedModels addObject:model];
......@@ -591,7 +592,7 @@ static CGSize AssetGridThumbnailSize;
NSMutableArray *assets = [NSMutableArray arrayWithCapacity:indexPaths.count];
for (NSIndexPath *indexPath in indexPaths) {
TZAssetModel *model = _photoArr[indexPath.item];
TZAssetModel *model = _models[indexPath.item];
[assets addObject:model.asset];
}
......
......@@ -10,13 +10,14 @@
@interface TZPhotoPreviewController : UIViewController
@property (nonatomic, strong) NSArray *photoArr; ///< All photos / 所有图片的数组
// @property (nonatomic, strong) NSMutableArray *selectedPhotoArr; ///< Current selected photos / 当前选中的图片数组
@property (nonatomic, strong) NSMutableArray *models; ///< All photo models / 所有图片模型数组
@property (nonatomic, strong) NSMutableArray *photos; ///< All photos / 所有图片数组
@property (nonatomic, assign) NSInteger currentIndex; ///< Index of the photo user click / 用户点击的图片的索引
@property (nonatomic, assign) BOOL isSelectOriginalPhoto; ///< If YES,return original photo / 是否返回原图
/// Return the new selected photos / 返回最新的选中图片数组
@property (nonatomic, copy) void (^backButtonClickBlock)(BOOL isSelectOriginalPhoto);
@property (nonatomic, copy) void (^okButtonClickBlock)(BOOL isSelectOriginalPhoto);
@property (nonatomic, copy) void (^okButtonClickBlockWithPreviewType)(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto);
@end
......@@ -16,6 +16,8 @@
@interface TZPhotoPreviewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate> {
UICollectionView *_collectionView;
BOOL _isHideNaviBar;
NSArray *_photosTemp;
NSArray *_assetsTemp;
UIView *_naviBar;
UIButton *_backButton;
......@@ -37,11 +39,21 @@
[super viewDidLoad];
__weak typeof(self) weakSelf = self;
_tzImagePickerVc = (TZImagePickerController *)weakSelf.navigationController;
if (!self.models.count) {
self.models = [NSMutableArray arrayWithArray:_tzImagePickerVc.selectedModels];
_assetsTemp = [NSMutableArray arrayWithArray:_tzImagePickerVc.selectedAssets];
self.isSelectOriginalPhoto = _tzImagePickerVc.isSelectOriginalPhoto;
}
[self configCollectionView];
[self configCustomNaviBar];
[self configBottomToolBar];
}
- (void)setPhotos:(NSMutableArray *)photos {
_photos = photos;
_photosTemp = [NSArray arrayWithArray:photos];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
......@@ -149,7 +161,7 @@
_collectionView.scrollsToTop = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.contentOffset = CGPointMake(0, 0);
_collectionView.contentSize = CGSizeMake(self.view.tz_width * _photoArr.count, self.view.tz_height);
_collectionView.contentSize = CGSizeMake(self.view.tz_width * _models.count, self.view.tz_height);
[self.view addSubview:_collectionView];
[_collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
}
......@@ -157,7 +169,7 @@
#pragma mark - Click Event
- (void)select:(UIButton *)selectButton {
TZAssetModel *model = _photoArr[_currentIndex];
TZAssetModel *model = _models[_currentIndex];
if (!selectButton.isSelected) {
// 1. select:check if over the maxImagesCount / 选择照片,检查是否超过了最大个数的限制
if (_tzImagePickerVc.selectedModels.count >= _tzImagePickerVc.maxImagesCount) {
......@@ -166,6 +178,10 @@
// 2. if not over the maxImagesCount / 如果没有超过最大个数限制
} else {
[_tzImagePickerVc.selectedModels addObject:model];
if (self.photos) {
[self.tzImagePickerVc.selectedAssets addObject:_assetsTemp[_currentIndex]];
[self.photos addObject:_photosTemp[_currentIndex]];
}
if (model.type == TZAssetModelMediaTypeVideo) {
[_tzImagePickerVc showAlertWithTitle:@"多选状态下选择视频,默认将视频当图片发送"];
}
......@@ -175,6 +191,10 @@
for (TZAssetModel *model_item in selectedModels) {
if ([model.asset isEqual:model_item.asset]) {
[self.tzImagePickerVc.selectedModels removeObject:model_item];
if (self.photos) {
[self.tzImagePickerVc.selectedAssets removeObject:_assetsTemp[_currentIndex]];
[self.photos removeObject:_photosTemp[_currentIndex]];
}
}
}
}
......@@ -187,6 +207,10 @@
}
- (void)back {
if (self.navigationController.childViewControllers.count < 2) {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
return;
}
[self.navigationController popViewControllerAnimated:YES];
if (self.backButtonClickBlock) {
self.backButtonClickBlock(_isSelectOriginalPhoto);
......@@ -195,12 +219,15 @@
- (void)okButtonClick {
if (_tzImagePickerVc.selectedModels.count == 0) {
TZAssetModel *model = _photoArr[_currentIndex];
TZAssetModel *model = _models[_currentIndex];
[_tzImagePickerVc.selectedModels addObject:model];
}
if (self.okButtonClickBlock) {
self.okButtonClickBlock(_isSelectOriginalPhoto);
}
if (self.okButtonClickBlockWithPreviewType) {
self.okButtonClickBlockWithPreviewType(self.photos,self.tzImagePickerVc.selectedAssets,self.isSelectOriginalPhoto);
}
}
- (void)originalPhotoButtonClick {
......@@ -224,12 +251,12 @@
#pragma mark - UICollectionViewDataSource && Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _photoArr.count;
return _models.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TZPhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZPhotoPreviewCell" forIndexPath:indexPath];
cell.model = _photoArr[indexPath.row];
cell.model = _models[indexPath.row];
__block BOOL _weakIsHideNaviBar = _isHideNaviBar;
__weak typeof(_naviBar) weakNaviBar = _naviBar;
......@@ -248,7 +275,7 @@
#pragma mark - Private Method
- (void)refreshNaviBarAndBottomBarState {
TZAssetModel *model = _photoArr[_currentIndex];
TZAssetModel *model = _models[_currentIndex];
_selectButton.selected = model.isSelected;
_numberLable.text = [NSString stringWithFormat:@"%zd",_tzImagePickerVc.selectedModels.count];
_numberImageView.hidden = (_tzImagePickerVc.selectedModels.count <= 0 || _isHideNaviBar);
......@@ -271,7 +298,7 @@
}
- (void)showPhotoBytes {
[[TZImageManager manager] getPhotosBytesWithArray:@[_photoArr[_currentIndex]] completion:^(NSString *totalBytes) {
[[TZImageManager manager] getPhotosBytesWithArray:@[_models[_currentIndex]] completion:^(NSString *totalBytes) {
_originalPhotoLable.text = [NSString stringWithFormat:@"(%@)",totalBytes];
}];
}
......
......@@ -18,6 +18,7 @@
@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate> {
NSMutableArray *_selectedPhotos;
NSMutableArray *_selectedAssets;
BOOL _isSelectOriginalPhoto;
CGFloat _itemWH;
CGFloat _margin;
......@@ -77,6 +78,19 @@
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == _selectedPhotos.count) {
[self pickPhotoButtonClick:nil];
} else { // preview photos / 预览照片
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
// imagePickerVc.allowPickingOriginalPhoto = NO;
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
_selectedAssets = [NSMutableArray arrayWithArray:assets];
_isSelectOriginalPhoto = isSelectOriginalPhoto;
_layout.itemCount = _selectedPhotos.count;
[_collectionView reloadData];
_collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
}];
[self presentViewController:imagePickerVc animated:YES completion:nil];
}
}
......@@ -107,6 +121,7 @@
- (IBAction)pickPhotoButtonClick:(UIButton *)sender {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的
// You can get the photos by block, the same as by delegate.
......@@ -143,6 +158,7 @@
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
_selectedAssets = [NSMutableArray arrayWithArray:assets];
_isSelectOriginalPhoto = isSelectOriginalPhoto;
_layout.itemCount = _selectedPhotos.count;
[_collectionView reloadData];
_collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
......
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