//
|
// PhotosPage.m
|
// GoldRich
|
//
|
// Created by WindShan on 2017/2/22.
|
// Copyright © 2017年 WindShan. All rights reserved.
|
//
|
|
#import "PhotosPage.h"
|
|
#import "TZImagePickerController.h"
|
#import "UIView+Layout.h"
|
#import "TZTestCell.h"
|
#import <AssetsLibrary/AssetsLibrary.h>
|
#import <Photos/Photos.h>
|
#import "LxGridViewFlowLayout.h"
|
#import "TZImageManager.h"
|
#import "TZVideoPlayerController.h"
|
#import "TZPhotoPreviewController.h"
|
#import "TZGifPhotoPreviewController.h"
|
#import "MBProgressHUD+MP.h"
|
|
#define MAX_COUNTTF 9
|
|
@interface PhotosPage ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UINavigationControllerDelegate>
|
|
{
|
NSMutableArray *_selectedPhotos;
|
NSMutableArray *_selectedAssets;
|
BOOL _isSelectOriginalPhoto; // 是否上传原图
|
|
CGFloat _itemWH;
|
CGFloat _margin;
|
}
|
|
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
|
@property (nonatomic, strong) UICollectionView *collectionView;
|
|
@property (nonatomic, assign) int maxCountTF; ///< 照片最大可选张数,设置为1即为单选模式
|
@property (nonatomic, assign) int columnNumberTF;// 每行展示的张数
|
|
@end
|
|
@implementation PhotosPage
|
|
- (void)viewDidLoad
|
{
|
[super viewDidLoad];
|
|
self.navigationItem.title = @"分享";
|
[self setNavigationLeft:@"返回" sel:@selector(backAction)];
|
_maxCountTF = MAX_COUNTTF;
|
_columnNumberTF = 4;
|
|
self.view.backgroundColor = [UIColor whiteColor];
|
_selectedPhotos = [NSMutableArray array];
|
_selectedAssets = [NSMutableArray array];
|
[self configCollectionView];
|
}
|
|
- (void)didReceiveMemoryWarning
|
{
|
[super didReceiveMemoryWarning];
|
// Dispose of any resources that can be recreated.
|
}
|
|
-(void)backAction
|
{
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
}
|
|
#pragma clang diagnostic push
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
- (UIImagePickerController *)imagePickerVc
|
{
|
if (_imagePickerVc == nil)
|
{
|
_imagePickerVc = [[UIImagePickerController alloc] init];
|
_imagePickerVc.delegate = self;
|
// set appearance / 改变相册选择页的导航栏外观
|
_imagePickerVc.navigationBar.barTintColor = self.navigationController.navigationBar.barTintColor;
|
_imagePickerVc.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
|
UIBarButtonItem *tzBarItem, *BarItem;
|
if (iOS9Later)
|
{
|
tzBarItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[TZImagePickerController class]]];
|
BarItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UIImagePickerController class]]];
|
}
|
else
|
{
|
tzBarItem = [UIBarButtonItem appearanceWhenContainedIn:[TZImagePickerController class], nil];
|
BarItem = [UIBarButtonItem appearanceWhenContainedIn:[UIImagePickerController class], nil];
|
}
|
|
NSDictionary *titleTextAttributes = [tzBarItem titleTextAttributesForState:UIControlStateNormal];
|
[BarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
|
}
|
return _imagePickerVc;
|
}
|
|
- (BOOL)prefersStatusBarHidden
|
{
|
return NO;
|
}
|
|
- (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;
|
|
CGFloat rgb = 244 / 255.0;
|
self.view.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
|
|
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.tz_width, self.view.tz_height-150) collectionViewLayout:layout];
|
|
_collectionView.alwaysBounceVertical = YES;
|
_collectionView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
|
_collectionView.contentInset = UIEdgeInsetsMake(4, 4, 4, 4);
|
_collectionView.dataSource = self;
|
_collectionView.delegate = self;
|
_collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
|
[self.view addSubview:_collectionView];
|
[_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
|
|
UIButton* shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
shareBtn.frame = CGRectMake(10, _collectionView.frame.size.height+20, SCREEN_WIDTH-20, 50);
|
[shareBtn setTitle:@"分享" forState:UIControlStateNormal];
|
shareBtn.backgroundColor = RgbColor(85, 81, 168);
|
shareBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
|
[shareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
shareBtn.layer.masksToBounds=YES;
|
shareBtn.layer.cornerRadius=5.0f;
|
[shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
|
[self.view addSubview:shareBtn];
|
}
|
|
-(void)shareAction
|
{
|
if(_selectedPhotos.count > 0)
|
{
|
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:_selectedPhotos applicationActivities:nil];
|
|
//不出现在活动项目
|
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
|
UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
|
[self presentViewController:activityVC animated:TRUE completion:nil];
|
}
|
else
|
{
|
[MBProgressHUD showAutoMessage:@"请先设置需要分享的图片!3" ToView:self.navigationController.view];
|
}
|
}
|
|
#pragma mark UICollectionView
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
return _selectedPhotos.count + 1;
|
}
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
|
cell.videoImageView.hidden = YES;
|
if (indexPath.row == _selectedPhotos.count)
|
{
|
cell.imageView.image = [UIImage imageNamed:@"AlbumAddBtn.png"];
|
cell.deleteBtn.hidden = YES;
|
cell.gifLable.hidden = YES;
|
}
|
else
|
{
|
cell.imageView.image = _selectedPhotos[indexPath.row];
|
cell.asset = _selectedAssets[indexPath.row];
|
cell.deleteBtn.hidden = NO;
|
}
|
|
// 判断是否准许选择gif图片
|
// if (!self.allowPickingGifSwitch.isOn)
|
// {
|
// cell.gifLable.hidden = YES;
|
// }
|
|
cell.deleteBtn.tag = indexPath.row;
|
[cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
|
return cell;
|
}
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
if (indexPath.row == _selectedPhotos.count)
|
{
|
BOOL showSheet = YES; // 是否把拍照按钮放外面
|
if (showSheet)
|
{
|
if(self.maxCountTF > 0)
|
{
|
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"去相册选择", nil];
|
[sheet showInView:self.view];
|
}
|
else
|
{
|
[MBProgressHUD showAutoMessage:@"最多分享9张图片!" ToView:self.navigationController.view];
|
}
|
}
|
else
|
{
|
[self pushImagePickerController];
|
}
|
}
|
else
|
{
|
// preview photos or video / 预览照片或者视频
|
id asset = _selectedAssets[indexPath.row];
|
BOOL isVideo = NO;
|
if ([asset isKindOfClass:[PHAsset class]])
|
{
|
PHAsset *phAsset = asset;
|
isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
|
}
|
else if ([asset isKindOfClass:[ALAsset class]])
|
{
|
ALAsset *alAsset = asset;
|
isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
|
}
|
|
// if ([[asset valueForKey:@"filename"] containsString:@"GIF"] && self.allowPickingGifSwitch.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 / 预览视频
|
// TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
|
// TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
|
// vc.model = model;
|
// [self presentViewController:vc animated:YES completion:nil];
|
// }
|
// else
|
{ // preview photos / 预览照片
|
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
|
imagePickerVc.maxImagesCount = self.maxCountTF;
|
imagePickerVc.allowPickingOriginalPhoto = YES; //准许选择原图
|
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
|
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto)
|
{
|
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
|
_selectedAssets = [NSMutableArray arrayWithArray:assets];
|
_isSelectOriginalPhoto = isSelectOriginalPhoto;
|
[_collectionView reloadData];
|
_collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
|
}];
|
|
[self presentViewController:imagePickerVc animated:YES completion:nil];
|
}
|
}
|
}
|
|
#pragma mark - LxGridViewDataSource
|
|
/// 以下三个方法为长按排序相关代码
|
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
return indexPath.item < _selectedPhotos.count;
|
}
|
|
- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath canMoveToIndexPath:(NSIndexPath *)destinationIndexPath
|
{
|
return (sourceIndexPath.item < _selectedPhotos.count && destinationIndexPath.item < _selectedPhotos.count);
|
}
|
|
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath didMoveToIndexPath:(NSIndexPath *)destinationIndexPath {
|
UIImage *image = _selectedPhotos[sourceIndexPath.item];
|
[_selectedPhotos removeObjectAtIndex:sourceIndexPath.item];
|
[_selectedPhotos insertObject:image atIndex:destinationIndexPath.item];
|
|
id asset = _selectedAssets[sourceIndexPath.item];
|
[_selectedAssets removeObjectAtIndex:sourceIndexPath.item];
|
[_selectedAssets insertObject:asset atIndex:destinationIndexPath.item];
|
|
[_collectionView reloadData];
|
}
|
|
#pragma mark - TZImagePickerController
|
|
- (void)pushImagePickerController
|
{
|
if (self.maxCountTF <= 0)
|
{
|
return;
|
}
|
|
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:MAX_COUNTTF columnNumber:self.columnNumberTF delegate:self pushPhotoPickerVc:YES];
|
|
|
#pragma mark - 四类个性化设置,这些参数都可以不传,此时会走默认设置
|
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
|
|
if (self.maxCountTF > 1)
|
{
|
// 1.设置目前已经选中的图片数组
|
imagePickerVc.selectedAssets = _selectedAssets; // 目前已经选中的图片数组
|
}
|
|
imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
|
|
// 2. Set the appearance
|
// 2. 在这里设置imagePickerVc的外观
|
// imagePickerVc.navigationBar.barTintColor = [UIColor greenColor];
|
// imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
|
// imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
|
// imagePickerVc.navigationBar.translucent = NO;
|
|
// 3. Set allow picking video & photo & originalPhoto or not
|
// 3. 设置是否可以选择视频/图片/原图
|
imagePickerVc.allowPickingVideo = NO;
|
imagePickerVc.allowPickingImage = YES;
|
imagePickerVc.allowPickingOriginalPhoto = YES;// 是否可以选择原图
|
imagePickerVc.allowPickingGif = NO; // 是否可以选择Gif 图片
|
|
// 4. 照片排列按修改时间升序
|
imagePickerVc.sortAscendingByModificationDate = YES;
|
|
// imagePickerVc.minImagesCount = 3;
|
// imagePickerVc.alwaysEnableDoneBtn = YES;
|
|
// imagePickerVc.minPhotoWidthSelectable = 3000;
|
// imagePickerVc.minPhotoHeightSelectable = 2000;
|
|
/// 5. Single selection mode, valid when maxImagesCount = 1
|
/// 5. 单选模式,maxImagesCount为1时才生效
|
imagePickerVc.showSelectBtn = NO;
|
imagePickerVc.allowCrop = NO; // 是否准许裁剪
|
imagePickerVc.needCircleCrop = NO;//是否需要圆形裁剪框
|
imagePickerVc.circleCropRadius = 100;
|
|
/*
|
[imagePickerVc setCropViewSettingBlock:^(UIView *cropView) {
|
cropView.layer.borderColor = [UIColor redColor].CGColor;
|
cropView.layer.borderWidth = 2.0;
|
}];*/
|
|
//imagePickerVc.allowPreview = NO;
|
#pragma mark - 到这里为止
|
|
// You can get the photos by block, the same as by delegate.
|
// 你可以通过block或者代理,来得到用户选择的照片.
|
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto)
|
{
|
|
self.maxCountTF = MAX_COUNTTF-[[NSString stringWithFormat:@"%lu",photos.count] intValue];
|
LOG_INFO(@"当前可以选择图片数目:%d",self.maxCountTF);
|
}];
|
|
[self presentViewController:imagePickerVc animated:YES completion:nil];
|
}
|
|
#pragma mark - UIImagePickerController
|
|
- (void)takePhoto
|
{
|
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS7Later)
|
{
|
// 无相机权限 做一个友好的提示
|
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
|
[alert show];
|
// 拍照之前还需要检查相册权限
|
} else if ([[TZImageManager manager] authorizationStatus] == 2) { // 已被拒绝,没有相册权限,将无法保存拍的照片
|
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
|
alert.tag = 1;
|
[alert show];
|
}
|
else if ([[TZImageManager manager] authorizationStatus] == 0)
|
{
|
// 正在弹框询问用户是否允许访问相册,监听权限状态
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
return [self takePhoto];
|
});
|
}
|
else
|
{ // 调用相机
|
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
|
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
|
{
|
self.imagePickerVc.sourceType = sourceType;
|
if(iOS8Later) {
|
_imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
|
}
|
[self presentViewController:_imagePickerVc animated:YES completion:nil];
|
}
|
else
|
{
|
NSLog(@"模拟器中无法打开照相机,请在真机中使用");
|
}
|
}
|
}
|
|
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
{
|
[picker dismissViewControllerAnimated:YES completion:nil];
|
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
|
if ([type isEqualToString:@"public.image"])
|
{
|
TZImagePickerController *tzImagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
|
tzImagePickerVc.sortAscendingByModificationDate = YES;
|
[tzImagePickerVc showProgressHUD];
|
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
|
// save photo and get asset / 保存图片,获取到asset
|
[[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error)
|
{
|
if (error)
|
{
|
[tzImagePickerVc hideProgressHUD];
|
NSLog(@"图片保存失败 %@",error);
|
}
|
else
|
{
|
[[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model)
|
{
|
[[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models)
|
{
|
[tzImagePickerVc hideProgressHUD];
|
TZAssetModel *assetModel = [models firstObject];
|
if (tzImagePickerVc.sortAscendingByModificationDate)
|
{
|
assetModel = [models lastObject];
|
}
|
|
if (/* DISABLES CODE */ (NO))
|
{
|
// 允许裁剪,去裁剪
|
TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initCropTypeWithAsset:assetModel.asset photo:image completion:^(UIImage *cropImage, id asset)
|
{
|
[self refreshCollectionViewWithAddedAsset:asset image:cropImage];
|
}];
|
|
imagePicker.needCircleCrop = NO;
|
imagePicker.circleCropRadius = 100;
|
[self presentViewController:imagePicker animated:YES completion:nil];
|
}
|
else
|
{
|
[self refreshCollectionViewWithAddedAsset:assetModel.asset image:image];
|
}
|
}];
|
}];
|
}
|
}];
|
}
|
}
|
|
- (void)refreshCollectionViewWithAddedAsset:(id)asset image:(UIImage *)image
|
{
|
[_selectedAssets addObject:asset];
|
[_selectedPhotos addObject:image];
|
[_collectionView reloadData];
|
|
self.maxCountTF = MAX_COUNTTF - [[NSString stringWithFormat:@"%lu",_selectedPhotos.count] intValue];
|
LOG_INFO(@"当前可以选择图片数目:%d",self.maxCountTF);
|
|
|
}
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
|
{
|
if ([picker isKindOfClass:[UIImagePickerController class]]) {
|
[picker dismissViewControllerAnimated:YES completion:nil];
|
}
|
}
|
|
#pragma mark - UIActionSheetDelegate
|
|
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
|
if (buttonIndex == 0) { // take photo / 去拍照
|
[self takePhoto];
|
} else if (buttonIndex == 1) {
|
[self pushImagePickerController];
|
}
|
}
|
|
#pragma mark - UIAlertViewDelegate
|
|
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
|
if (buttonIndex == 1) { // 去设置界面,开启相机访问权限
|
if (iOS8Later) {
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
|
} else {
|
NSURL *privacyUrl;
|
if (alertView.tag == 1) {
|
privacyUrl = [NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"];
|
} else {
|
privacyUrl = [NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"];
|
}
|
if ([[UIApplication sharedApplication] canOpenURL:privacyUrl]) {
|
[[UIApplication sharedApplication] openURL:privacyUrl];
|
} else {
|
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"抱歉" message:@"无法跳转到隐私设置页面,请手动前往设置页面,谢谢" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
|
[alert show];
|
}
|
}
|
}
|
}
|
|
#pragma mark - TZImagePickerControllerDelegate
|
|
/// User click cancel button
|
/// 用户点击了取消
|
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
|
// NSLog(@"cancel");
|
}
|
|
// The picker should dismiss itself; when it dismissed these handle will be called.
|
// If isOriginalPhoto is YES, user picked the original photo.
|
// You can get original photo with asset, by the method [[TZImageManager manager] getOriginalPhotoWithAsset:completion:].
|
// The UIImage Object in photos default width is 828px, you can set it by photoWidth property.
|
// 这个照片选择器会自己dismiss,当选择器dismiss的时候,会执行下面的代理方法
|
// 如果isSelectOriginalPhoto为YES,表明用户选择了原图
|
// 你可以通过一个asset获得原图,通过这个方法:[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]
|
// photos数组里的UIImage对象,默认是828像素宽,你可以通过设置photoWidth属性的值来改变它
|
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
|
_selectedPhotos = [NSMutableArray arrayWithArray:photos];
|
_selectedAssets = [NSMutableArray arrayWithArray:assets];
|
_isSelectOriginalPhoto = isSelectOriginalPhoto;
|
[_collectionView reloadData];
|
// _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
|
|
// 1.打印图片名字
|
[self printAssetsName:assets];
|
}
|
|
// If user picking a video, this callback will be called.
|
// If system version > iOS8,asset is kind of PHAsset class, else is ALAsset class.
|
// 如果用户选择了一个视频,下面的handle会被执行
|
// 如果系统版本大于iOS8,asset是PHAsset类的对象,否则是ALAsset类的对象
|
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset {
|
_selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
|
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
|
// open this code to send video / 打开这段代码发送视频
|
// [[TZImageManager manager] getVideoOutputPathWithAsset:asset completion:^(NSString *outputPath) {
|
// NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
|
// Export completed, send video here, send by outputPath or NSData
|
// 导出完成,在这里写上传代码,通过路径或者通过NSData上传
|
|
// }];
|
[_collectionView reloadData];
|
// _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
|
}
|
|
// If user picking a gif image, this callback will be called.
|
// 如果用户选择了一个gif图片,下面的handle会被执行
|
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(id)asset {
|
_selectedPhotos = [NSMutableArray arrayWithArray:@[animatedImage]];
|
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
|
[_collectionView reloadData];
|
}
|
|
#pragma mark - Click Event
|
|
- (void)deleteBtnClik:(UIButton *)sender
|
{
|
[_selectedPhotos removeObjectAtIndex:sender.tag];
|
[_selectedAssets removeObjectAtIndex:sender.tag];
|
|
[_collectionView performBatchUpdates:^
|
{
|
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
|
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
|
}
|
completion:^(BOOL finished)
|
{
|
self.maxCountTF = MAX_COUNTTF-[[NSString stringWithFormat:@"%lu",_selectedPhotos.count] intValue];
|
LOG_INFO(@"当前可以选择图片数目:%d",self.maxCountTF);
|
[_collectionView reloadData];
|
}];
|
}
|
//
|
//- (IBAction)showTakePhotoBtnSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// [_showSheetSwitch setOn:NO animated:YES];
|
// [_allowPickingImageSwitch setOn:YES animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)showSheetSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// [_showTakePhotoBtnSwitch setOn:NO animated:YES];
|
// [_allowPickingImageSwitch setOn:YES animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)allowPickingOriginPhotoSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// [_allowPickingImageSwitch setOn:YES animated:YES];
|
// [self.needCircleCropSwitch setOn:NO animated:YES];
|
// [self.allowCropSwitch setOn:NO animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)allowPickingImageSwitchClick:(UISwitch *)sender {
|
// if (!sender.isOn) {
|
// [_allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
|
// [_showTakePhotoBtnSwitch setOn:NO animated:YES];
|
// [_allowPickingVideoSwitch setOn:YES animated:YES];
|
// [_allowPickingGifSwitch setOn:NO animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)allowPickingGifSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// [_allowPickingImageSwitch setOn:YES animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)allowPickingVideoSwitchClick:(UISwitch *)sender {
|
// if (!sender.isOn) {
|
// [_allowPickingImageSwitch setOn:YES animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)allowCropSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// self.maxCountTF.text = @"1";
|
// [self.allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
|
// } else {
|
// [self.needCircleCropSwitch setOn:NO animated:YES];
|
// }
|
//}
|
//
|
//- (IBAction)needCircleCropSwitchClick:(UISwitch *)sender {
|
// if (sender.isOn) {
|
// [self.allowCropSwitch setOn:YES animated:YES];
|
// self.maxCountTF.text = @"1";
|
// [self.allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
|
// }
|
//}
|
|
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
[self.view endEditing:YES];
|
}
|
|
#pragma mark - Private
|
|
/// 打印图片名字
|
- (void)printAssetsName:(NSArray *)assets
|
{
|
NSString *fileName;
|
for (id asset in assets)
|
{
|
if ([asset isKindOfClass:[PHAsset class]]) {
|
PHAsset *phAsset = (PHAsset *)asset;
|
fileName = [phAsset valueForKey:@"filename"];
|
} else if ([asset isKindOfClass:[ALAsset class]]) {
|
ALAsset *alAsset = (ALAsset *)asset;
|
fileName = alAsset.defaultRepresentation.filename;;
|
}
|
//NSLog(@"图片名字:%@",fileName);
|
}
|
}
|
|
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
|
return UIInterfaceOrientationMaskPortrait;
|
}
|
#pragma clang diagnostic pop
|
|
|
/*
|
#pragma mark - Navigation
|
|
// In a storyboard-based application, you will often want to do a little preparation before navigation
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
// Get the new view controller using [segue destinationViewController].
|
// Pass the selected object to the new view controller.
|
}
|
*/
|
|
@end
|