Commit 1fe6744e authored by yuzheng.tz's avatar yuzheng.tz

Demo页支持横屏;支持视频&gif图片多选;支持同时选择图片/视频/gif图

parent 39467b90
......@@ -25,6 +25,7 @@ typedef enum : NSUInteger {
@property (nonatomic, copy) void (^didSelectPhotoBlock)(BOOL);
@property (nonatomic, assign) TZAssetCellType type;
@property (nonatomic, assign) BOOL allowPickingGif;
@property (nonatomic, assign) BOOL allowPickingMultipleVideo;
@property (nonatomic, copy) NSString *representedAssetIdentifier;
@property (nonatomic, assign) int32_t imageRequestID;
......
......@@ -69,6 +69,7 @@
if (model.isSelected) {
[self fetchBigImage];
}
[self setNeedsLayout];
}
- (void)setShowSelectBtn:(BOOL)showSelectBtn {
......@@ -83,26 +84,28 @@
- (void)setType:(TZAssetCellType)type {
_type = type;
if (type == TZAssetCellTypePhoto || type == TZAssetCellTypeLivePhoto || (type == TZAssetCellTypePhotoGif && !self.allowPickingGif)) {
if (type == TZAssetCellTypePhoto || type == TZAssetCellTypeLivePhoto || (type == TZAssetCellTypePhotoGif && !self.allowPickingGif) || self.allowPickingMultipleVideo) {
_selectImageView.hidden = NO;
_selectPhotoButton.hidden = NO;
_bottomView.hidden = YES;
} else { // Video of Gif
_selectImageView.hidden = YES;
_selectPhotoButton.hidden = YES;
_bottomView.hidden = NO;
}
if (type == TZAssetCellTypeVideo) {
self.bottomView.hidden = NO;
self.timeLength.text = _model.timeLength;
self.videoImgView.hidden = NO;
_timeLength.tz_left = self.videoImgView.tz_right;
_timeLength.textAlignment = NSTextAlignmentRight;
} else {
} else if (type == TZAssetCellTypePhotoGif) {
self.bottomView.hidden = NO;
self.timeLength.text = @"GIF";
self.videoImgView.hidden = YES;
_timeLength.tz_left = 5;
_timeLength.textAlignment = NSTextAlignmentLeft;
}
}
}
- (void)selectPhotoButtonClick:(UIButton *)sender {
......
......@@ -10,6 +10,7 @@
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import "TZAssetModel.h"
@class TZAlbumModel,TZAssetModel;
@protocol TZImagePickerControllerDelegate;
......@@ -98,6 +99,9 @@
/// 修正图片转向
- (UIImage *)fixOrientation:(UIImage *)aImage;
/// 获取asset的资源类型
- (TZAssetModelMediaType)getAssetType:(id)asset;
@end
//@interface TZSortDescriptor : NSSortDescriptor
......
......@@ -315,24 +315,13 @@ static dispatch_once_t onceToken;
if (!canSelect) return nil;
TZAssetModel *model;
TZAssetModelMediaType type = TZAssetModelMediaTypePhoto;
TZAssetModelMediaType type = [self getAssetType:asset];
if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = (PHAsset *)asset;
if (phAsset.mediaType == PHAssetMediaTypeVideo) type = TZAssetModelMediaTypeVideo;
else if (phAsset.mediaType == PHAssetMediaTypeAudio) type = TZAssetModelMediaTypeAudio;
else if (phAsset.mediaType == PHAssetMediaTypeImage) {
if (iOS9_1Later) {
// if (asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive) type = TZAssetModelMediaTypeLivePhoto;
}
// Gif
if ([[phAsset valueForKey:@"filename"] hasSuffix:@"GIF"]) {
type = TZAssetModelMediaTypePhotoGif;
}
}
if (!allowPickingVideo && type == TZAssetModelMediaTypeVideo) return nil;
if (!allowPickingImage && type == TZAssetModelMediaTypePhoto) return nil;
if (!allowPickingImage && type == TZAssetModelMediaTypePhotoGif) return nil;
PHAsset *phAsset = (PHAsset *)asset;
if (self.hideWhenCanNotSelect) {
// 过滤掉尺寸不满足要求的图片
if (![self isPhotoSelectableWithAsset:phAsset]) {
......@@ -348,8 +337,7 @@ static dispatch_once_t onceToken;
return model;
}
/// Allow picking video
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo;
if (type == TZAssetModelMediaTypeVideo) {
NSTimeInterval duration = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue];
NSString *timeLength = [NSString stringWithFormat:@"%0.0f",duration];
timeLength = [self getNewTimeFromDurationSecond:timeLength.integerValue];
......@@ -367,6 +355,29 @@ static dispatch_once_t onceToken;
return model;
}
- (TZAssetModelMediaType)getAssetType:(id)asset {
TZAssetModelMediaType type = TZAssetModelMediaTypePhoto;
if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = (PHAsset *)asset;
if (phAsset.mediaType == PHAssetMediaTypeVideo) type = TZAssetModelMediaTypeVideo;
else if (phAsset.mediaType == PHAssetMediaTypeAudio) type = TZAssetModelMediaTypeAudio;
else if (phAsset.mediaType == PHAssetMediaTypeImage) {
if (iOS9_1Later) {
// if (asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive) type = TZAssetModelMediaTypeLivePhoto;
}
// Gif
if ([[phAsset valueForKey:@"filename"] hasSuffix:@"GIF"]) {
type = TZAssetModelMediaTypePhotoGif;
}
}
} else {
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
type = TZAssetModelMediaTypeVideo;
}
}
return type;
}
- (NSString *)getNewTimeFromDurationSecond:(NSInteger)duration {
NSString *newTime;
if (duration < 10) {
......
......@@ -71,6 +71,8 @@
/// Default is YES, if set NO, user can't picking video.
/// 默认为YES,如果设置为NO,用户将不能选择视频
@property (nonatomic, assign) BOOL allowPickingVideo;
/// Default is NO / 默认为NO,为YES时可以多选视频/gif图片,和照片共享最大可选张数maxImagesCount的限制
@property (nonatomic, assign) BOOL allowPickingMultipleVideo;
/// Default is NO, if set YES, user can picking gif image.
/// 默认为NO,如果设置为YES,用户可以选择gif图片
......
......@@ -54,6 +54,7 @@
self.navigationBar.barTintColor = [UIColor colorWithRed:(34/255.0) green:(34/255.0) blue:(34/255.0) alpha:1.0];
self.navigationBar.tintColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
if (!TZ_isGlobalHideStatusBar) [UIApplication sharedApplication].statusBarHidden = NO;
}
}
......@@ -449,7 +450,7 @@
_selectedAssets = selectedAssets;
_selectedModels = [NSMutableArray array];
for (id asset in selectedAssets) {
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhoto];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:[[TZImageManager manager] getAssetType:asset]];
model.isSelected = YES;
[_selectedModels addObject:model];
}
......
......@@ -431,6 +431,7 @@ static CGFloat itemMargin = 5;
}
// the cell dipaly photo or video / 展示照片或视频的cell
TZAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZAssetCell" forIndexPath:indexPath];
cell.allowPickingMultipleVideo = tzImagePickerVc.allowPickingMultipleVideo;
cell.photoDefImageName = tzImagePickerVc.photoDefImageName;
cell.photoSelImageName = tzImagePickerVc.photoSelImageName;
TZAssetModel *model;
......@@ -492,7 +493,7 @@ static CGFloat itemMargin = 5;
index = indexPath.row - 1;
}
TZAssetModel *model = _models[index];
if (model.type == TZAssetModelMediaTypeVideo) {
if (model.type == TZAssetModelMediaTypeVideo && !tzImagePickerVc.allowPickingMultipleVideo) {
if (tzImagePickerVc.selectedModels.count > 0) {
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[imagePickerVc showAlertWithTitle:[NSBundle tz_localizedStringForKey:@"Can not choose both video and photo"]];
......@@ -501,7 +502,7 @@ static CGFloat itemMargin = 5;
videoPlayerVc.model = model;
[self.navigationController pushViewController:videoPlayerVc animated:YES];
}
} else if (model.type == TZAssetModelMediaTypePhotoGif && tzImagePickerVc.allowPickingGif) {
} else if (model.type == TZAssetModelMediaTypePhotoGif && tzImagePickerVc.allowPickingGif && !tzImagePickerVc.allowPickingMultipleVideo) {
if (tzImagePickerVc.selectedModels.count > 0) {
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[imagePickerVc showAlertWithTitle:[NSBundle tz_localizedStringForKey:@"Can not choose both photo and GIF"]];
......@@ -633,16 +634,21 @@ static CGFloat itemMargin = 5;
- (void)scrollCollectionViewToBottom {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_shouldScrollToBottom && _models.count > 0 && tzImagePickerVc.sortAscendingByModificationDate) {
NSInteger item = _models.count - 1;
if (_shouldScrollToBottom && _models.count > 0) {
NSInteger item = 0;
if (tzImagePickerVc.sortAscendingByModificationDate) {
item = _models.count - 1;
if (_showTakePhotoBtn) {
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (tzImagePickerVc.allowPickingImage && tzImagePickerVc.allowTakePicture) {
item += 1;
}
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
_shouldScrollToBottom = NO;
});
}
}
......@@ -720,7 +726,7 @@ static CGFloat itemMargin = 5;
TZPhotoPreviewController *photoPreviewVc = [[TZPhotoPreviewController alloc] init];
if (tzImagePickerVc.sortAscendingByModificationDate) {
photoPreviewVc.currentIndex = _models.count - 1;
}else{
} else {
photoPreviewVc.currentIndex = 0;
}
photoPreviewVc.models = _models;
......@@ -870,7 +876,7 @@ static CGFloat itemMargin = 5;
@implementation TZCollectionView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
if ( [view isKindOfClass:[UIControl class]]) {
if ([view isKindOfClass:[UIControl class]]) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
......
......@@ -8,11 +8,18 @@
#import <UIKit/UIKit.h>
@class TZAssetModel,TZProgressView,TZPhotoPreviewView;
@interface TZPhotoPreviewCell : UICollectionViewCell
@class TZAssetModel;
@interface TZAssetPreviewCell : UICollectionViewCell
@property (nonatomic, strong) TZAssetModel *model;
@property (nonatomic, copy) void (^singleTapGestureBlock)();
- (void)configSubviews;
- (void)photoPreviewCollectionViewDidScroll;
@end
@class TZAssetModel,TZProgressView,TZPhotoPreviewView;
@interface TZPhotoPreviewCell : TZAssetPreviewCell
@property (nonatomic, copy) void (^imageProgressUpdateBlock)(double progress);
@property (nonatomic, strong) TZPhotoPreviewView *previewView;
......@@ -43,3 +50,18 @@
- (void)recoverSubviews;
@end
@class AVPlayer, AVPlayerLayer;
@interface TZVideoPreviewCell : TZAssetPreviewCell
@property (strong, nonatomic) AVPlayer *player;
@property (strong, nonatomic) AVPlayerLayer *playerLayer;
@property (strong, nonatomic) UIButton *playButton;
@property (strong, nonatomic) UIImage *cover;
- (void)pausePlayerAndShowNaviBar;
@end
@interface TZGifPreviewCell : TZAssetPreviewCell
@property (strong, nonatomic) TZPhotoPreviewView *previewView;
@end
......@@ -12,16 +12,41 @@
#import "TZImageManager.h"
#import "TZProgressView.h"
#import "TZImageCropManager.h"
#import <MediaPlayer/MediaPlayer.h>
#import "TZImagePickerController.h"
@interface TZPhotoPreviewCell ()
@end
@implementation TZPhotoPreviewCell
@implementation TZAssetPreviewCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
[self configSubviews];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(photoPreviewCollectionViewDidScroll) name:@"photoPreviewCollectionViewDidScroll" object:nil];
}
return self;
}
- (void)configSubviews {
}
#pragma mark - Notification
- (void)photoPreviewCollectionViewDidScroll {
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@implementation TZPhotoPreviewCell
- (void)configSubviews {
self.previewView = [[TZPhotoPreviewView alloc] initWithFrame:CGRectZero];
__weak typeof(self) weakSelf = self;
[self.previewView setSingleTapGestureBlock:^{
......@@ -35,12 +60,10 @@
}
}];
[self addSubview:self.previewView];
}
return self;
}
- (void)setModel:(TZAssetModel *)model {
_model = model;
[super setModel:model];
_previewView.asset = model.asset;
}
......@@ -297,3 +320,128 @@
}
@end
@implementation TZVideoPreviewCell
- (void)configSubviews {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)configPlayButton {
if (_playButton) {
[_playButton removeFromSuperview];
}
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlay.png"] forState:UIControlStateNormal];
[_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlayHL.png"] forState:UIControlStateHighlighted];
[_playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_playButton];
}
- (void)setModel:(TZAssetModel *)model {
[super setModel:model];
[self configMoviePlayer];
}
- (void)configMoviePlayer {
if (_player) {
[_playerLayer removeFromSuperlayer];
_playerLayer = nil;
[_player pause];
_player = nil;
}
[[TZImageManager manager] getPhotoWithAsset:self.model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
_cover = photo;
}];
[[TZImageManager manager] getVideoWithAsset:self.model.asset completion:^(AVPlayerItem *playerItem, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
_player = [AVPlayer playerWithPlayerItem:playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.backgroundColor = [UIColor blackColor].CGColor;
_playerLayer.frame = self.bounds;
[self.layer addSublayer:_playerLayer];
[self configPlayButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
});
}];
}
- (void)layoutSubviews {
[super layoutSubviews];
_playerLayer.frame = self.bounds;
_playButton.frame = CGRectMake(0, 64, self.tz_width, self.tz_height - 64 - 44);
}
- (void)photoPreviewCollectionViewDidScroll {
[self pausePlayerAndShowNaviBar];
}
#pragma mark - Click Event
- (void)playButtonClick {
CMTime currentTime = _player.currentItem.currentTime;
CMTime durationTime = _player.currentItem.duration;
if (_player.rate == 0.0f) {
if (currentTime.value == durationTime.value) [_player.currentItem seekToTime:CMTimeMake(0, 1)];
[_player play];
[_playButton setImage:nil forState:UIControlStateNormal];
if (!TZ_isGlobalHideStatusBar && iOS7Later) {
[UIApplication sharedApplication].statusBarHidden = YES;
}
if (self.singleTapGestureBlock) {
self.singleTapGestureBlock();
}
} else {
[self pausePlayerAndShowNaviBar];
}
}
- (void)pausePlayerAndShowNaviBar {
if (_player.rate != 0.0) {
[_player pause];
[_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlay.png"] forState:UIControlStateNormal];
if (self.singleTapGestureBlock) {
self.singleTapGestureBlock();
}
}
}
@end
@implementation TZGifPreviewCell
- (void)configSubviews {
[self configPreviewView];
}
- (void)configPreviewView {
_previewView = [[TZPhotoPreviewView alloc] initWithFrame:CGRectZero];
__weak typeof(self) weakSelf = self;
[_previewView setSingleTapGestureBlock:^{
[weakSelf signleTapAction];
}];
[self addSubview:_previewView];
}
- (void)setModel:(TZAssetModel *)model {
[super setModel:model];
_previewView.model = self.model;
}
- (void)layoutSubviews {
[super layoutSubviews];
_previewView.frame = self.bounds;
}
#pragma mark - Click Event
- (void)signleTapAction {
if (self.singleTapGestureBlock) {
self.singleTapGestureBlock();
}
}
@end
......@@ -177,6 +177,8 @@
_collectionView.contentSize = CGSizeMake(self.models.count * (self.view.tz_width + 20), 0);
[self.view addSubview:_collectionView];
[_collectionView registerClass:[TZPhotoPreviewCell class] forCellWithReuseIdentifier:@"TZPhotoPreviewCell"];
[_collectionView registerClass:[TZVideoPreviewCell class] forCellWithReuseIdentifier:@"TZVideoPreviewCell"];
[_collectionView registerClass:[TZGifPreviewCell class] forCellWithReuseIdentifier:@"TZGifPreviewCell"];
}
- (void)configCropView {
......@@ -213,6 +215,7 @@
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
_naviBar.frame = CGRectMake(0, 0, self.view.tz_width, 64);
_backButton.frame = CGRectMake(10, 10, 44, 44);
......@@ -227,10 +230,11 @@
CGFloat offsetX = _offsetItemCount * _layout.itemSize.width;
[_collectionView setContentOffset:CGPointMake(offsetX, 0)];
}
if (_tzImagePickerVc.allowCrop) {
[_collectionView reloadData];
}
_toolBar.frame = CGRectMake(0, self.view.tz_height - 44, self.view.tz_width, 44);
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (_tzImagePickerVc.allowPickingOriginalPhoto) {
CGFloat fullImageWidth = [_tzImagePickerVc.fullImageBtnTitleStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.width;
_originalPhotoButton.frame = CGRectMake(0, 0, fullImageWidth + 56, 44);
......@@ -267,7 +271,7 @@
[_tzImagePickerVc.selectedAssets addObject:_assetsTemp[_currentIndex]];
[self.photos addObject:_photosTemp[_currentIndex]];
}
if (model.type == TZAssetModelMediaTypeVideo) {
if (model.type == TZAssetModelMediaTypeVideo && !_tzImagePickerVc.allowPickingMultipleVideo) {
[_tzImagePickerVc showAlertWithTitle:[NSBundle tz_localizedStringForKey:@"Select the video when in multi state, we will handle the video as a photo"]];
}
}
......@@ -369,6 +373,12 @@
}
}
- (void)didTapPreviewCell {
self.isHideNaviBar = !self.isHideNaviBar;
_naviBar.hidden = self.isHideNaviBar;
_toolBar.hidden = self.isHideNaviBar;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
......@@ -381,6 +391,8 @@
_currentIndex = currentIndex;
[self refreshNaviBarAndBottomBarState];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"photoPreviewCollectionViewDidScroll" object:nil];
}
#pragma mark - UICollectionViewDataSource && Delegate
......@@ -390,26 +402,24 @@
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TZPhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZPhotoPreviewCell" forIndexPath:indexPath];
cell.model = _models[indexPath.row];
TZImagePickerController *_tzImagePickerVc = (TZImagePickerController *)self.navigationController;
cell.cropRect = _tzImagePickerVc.cropRect;
cell.allowCrop = _tzImagePickerVc.allowCrop;
TZAssetModel *model = _models[indexPath.row];
TZAssetPreviewCell *cell;
__weak typeof(self) weakSelf = self;
if (!cell.singleTapGestureBlock) {
__weak typeof(_naviBar) weakNaviBar = _naviBar;
__weak typeof(_toolBar) weakToolBar = _toolBar;
cell.singleTapGestureBlock = ^(){
// show or hide naviBar / 显示或隐藏导航栏
weakSelf.isHideNaviBar = !weakSelf.isHideNaviBar;
weakNaviBar.hidden = weakSelf.isHideNaviBar;
weakToolBar.hidden = weakSelf.isHideNaviBar;
};
}
if (_tzImagePickerVc.allowPickingMultipleVideo && model.type == TZAssetModelMediaTypeVideo) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZVideoPreviewCell" forIndexPath:indexPath];
} else if (_tzImagePickerVc.allowPickingMultipleVideo && model.type == TZAssetModelMediaTypePhotoGif && _tzImagePickerVc.allowPickingGif) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZGifPreviewCell" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZPhotoPreviewCell" forIndexPath:indexPath];
TZPhotoPreviewCell *photoPreviewCell = (TZPhotoPreviewCell *)cell;
photoPreviewCell.cropRect = _tzImagePickerVc.cropRect;
photoPreviewCell.allowCrop = _tzImagePickerVc.allowCrop;
__weak typeof(_tzImagePickerVc) weakTzImagePickerVc = _tzImagePickerVc;
__weak typeof(_collectionView) weakCollectionView = _collectionView;
__weak typeof(cell) weakCell = cell;
[cell setImageProgressUpdateBlock:^(double progress) {
__weak typeof(photoPreviewCell) weakCell = photoPreviewCell;
[photoPreviewCell setImageProgressUpdateBlock:^(double progress) {
weakSelf.progress = progress;
if (progress >= 1) {
if (weakSelf.alertView && [weakCollectionView.visibleCells containsObject:weakCell]) {
......@@ -419,6 +429,12 @@
}
}
}];
}
cell.model = model;
[cell setSingleTapGestureBlock:^{
[weakSelf didTapPreviewCell];
}];
return cell;
}
......@@ -431,6 +447,8 @@
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[TZPhotoPreviewCell class]]) {
[(TZPhotoPreviewCell *)cell recoverSubviews];
} else if ([cell isKindOfClass:[TZVideoPreviewCell class]]) {
[(TZVideoPreviewCell *)cell pausePlayerAndShowNaviBar];
}
}
......
......@@ -41,6 +41,7 @@
self.navigationItem.title = tzImagePickerVc.previewBtnTitleStr;
}
[self configMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
......
......@@ -32,7 +32,6 @@
_deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteBtn setImage:[UIImage imageNamed:@"photo_delete"] forState:UIControlStateNormal];
_deleteBtn.frame = CGRectMake(self.tz_width - 36, 0, 36, 36);
_deleteBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, -10);
_deleteBtn.alpha = 0.6;
[self addSubview:_deleteBtn];
......@@ -43,7 +42,6 @@
_gifLable.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
_gifLable.textAlignment = NSTextAlignmentCenter;
_gifLable.font = [UIFont systemFontOfSize:10];
_gifLable.frame = CGRectMake(self.tz_width - 25, self.tz_height - 14, 25, 14);
[self addSubview:_gifLable];
}
return self;
......@@ -52,6 +50,8 @@
- (void)layoutSubviews {
[super layoutSubviews];
_imageView.frame = self.bounds;
_gifLable.frame = CGRectMake(self.tz_width - 25, self.tz_height - 14, 25, 14);
_deleteBtn.frame = CGRectMake(self.tz_width - 36, 0, 36, 36);
CGFloat width = self.tz_width / 3.0;
_videoImageView.frame = CGRectMake(width, width, width, width);
}
......
......@@ -29,8 +29,11 @@
}
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (strong, nonatomic) LxGridViewFlowLayout *layout;
@property (strong, nonatomic) CLLocation *location;
// 6个设置开关
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
// 设置开关
@property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 在内部显示拍照按钮
@property (weak, nonatomic) IBOutlet UISwitch *sortAscendingSwitch; ///< 照片排列按修改时间升序
@property (weak, nonatomic) IBOutlet UISwitch *allowPickingVideoSwitch; ///< 允许选择视频
......@@ -42,6 +45,7 @@
@property (weak, nonatomic) IBOutlet UITextField *columnNumberTF;
@property (weak, nonatomic) IBOutlet UISwitch *allowCropSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *needCircleCropSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *allowPickingMuitlpleVideoSwitch;
@end
@implementation ViewController
......@@ -83,13 +87,8 @@
- (void)configCollectionView {
// 如不需要长按排序效果,将LxGridViewFlowLayout类改成UICollectionViewFlowLayout即可
LxGridViewFlowLayout *layout = [[LxGridViewFlowLayout alloc] init];
_margin = 4;
_itemWH = (self.view.tz_width - 2 * _margin - 4) / 3 - _margin;
layout.itemSize = CGSizeMake(_itemWH, _itemWH);
layout.minimumInteritemSpacing = _margin;
layout.minimumLineSpacing = _margin;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 409, self.view.tz_width, self.view.tz_height - 409) collectionViewLayout:layout];
_layout = [[LxGridViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
CGFloat rgb = 244 / 255.0;
_collectionView.alwaysBounceVertical = YES;
_collectionView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
......@@ -101,6 +100,22 @@
[_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSInteger contentSizeH = 12 * 35 + 20;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.scrollView.contentSize = CGSizeMake(0, contentSizeH + 5);
});
_margin = 4;
_itemWH = (self.view.tz_width - 2 * _margin - 4) / 3 - _margin;
_layout.itemSize = CGSizeMake(_itemWH, _itemWH);
_layout.minimumInteritemSpacing = _margin;
_layout.minimumLineSpacing = _margin;
[self.collectionView setCollectionViewLayout:_layout];
self.collectionView.frame = CGRectMake(0, contentSizeH, self.view.tz_width, self.view.tz_height - contentSizeH);
}
#pragma mark UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
......@@ -146,12 +161,12 @@
ALAsset *alAsset = asset;
isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
}
if ([[asset valueForKey:@"filename"] containsString:@"GIF"] && self.allowPickingGifSwitch.isOn) {
if ([[asset valueForKey:@"filename"] containsString:@"GIF"] && self.allowPickingGifSwitch.isOn && !self.allowPickingMuitlpleVideoSwitch.isOn) {
TZGifPhotoPreviewController *vc = [[TZGifPhotoPreviewController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhotoGif timeLength:@""];
vc.model = model;
[self presentViewController:vc animated:YES completion:nil];
} else if (isVideo) { // perview video / 预览视频
} else if (isVideo && !self.allowPickingMuitlpleVideoSwitch.isOn) { // perview video / 预览视频
TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
vc.model = model;
......@@ -159,7 +174,9 @@
} else { // preview photos / 预览照片
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
imagePickerVc.maxImagesCount = self.maxCountTF.text.integerValue;
imagePickerVc.allowPickingGif = self.allowPickingGifSwitch.isOn;
imagePickerVc.allowPickingOriginalPhoto = self.allowPickingOriginalPhotoSwitch.isOn;
imagePickerVc.allowPickingMultipleVideo = self.allowPickingMuitlpleVideoSwitch.isOn;
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
......@@ -229,6 +246,7 @@
imagePickerVc.allowPickingImage = self.allowPickingImageSwitch.isOn;
imagePickerVc.allowPickingOriginalPhoto = self.allowPickingOriginalPhotoSwitch.isOn;
imagePickerVc.allowPickingGif = self.allowPickingGifSwitch.isOn;
imagePickerVc.allowPickingMultipleVideo = self.allowPickingMuitlpleVideoSwitch.isOn; // 是否可以多选视频
// 4. 照片排列按修改时间升序
imagePickerVc.sortAscendingByModificationDate = self.sortAscendingSwitch.isOn;
......@@ -578,12 +596,17 @@
- (IBAction)allowPickingGifSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
} else if (!self.allowPickingVideoSwitch.isOn) {
[self.allowPickingMuitlpleVideoSwitch setOn:NO animated:YES];
}
}
- (IBAction)allowPickingVideoSwitchClick:(UISwitch *)sender {
if (!sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
if (!self.allowPickingGifSwitch.isOn) {
[self.allowPickingMuitlpleVideoSwitch setOn:NO animated:YES];
}
}
}
......@@ -592,6 +615,9 @@
self.maxCountTF.text = @"1";
[self.allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
} else {
if ([self.maxCountTF.text isEqualToString:@"1"]) {
self.maxCountTF.text = @"9";
}
[self.needCircleCropSwitch setOn:NO animated:YES];
}
}
......@@ -604,6 +630,10 @@
}
}
- (IBAction)allowPickingMultipleVideoSwitchClick:(UISwitch *)sender {
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
......@@ -621,14 +651,10 @@
ALAsset *alAsset = (ALAsset *)asset;
fileName = alAsset.defaultRepresentation.filename;;
}
//NSLog(@"图片名字:%@",fileName);
// NSLog(@"图片名字:%@",fileName);
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
#pragma clang diagnostic pop
@end
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