TZImagePickerController 功能十分强大
#import "TZImagePickerController.h"
#import <Photos/Photos.h>
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"
#import "TZPhotoPreviewController.h"
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "FLAnimatedImage.h"
#import "TZImageUploadOperation.h"
使用方法 pod导入框架pod "TZImagePickerController"
导入头文件
#import <TZImagePickerController/TZImagePickerController.h>
1.简单使用
选择图片
TZImagePickerController *imagePicker = [[TZImagePickerController alloc]initWithMaxImagesCount:self.selectMaxImageCount columnNumber:4 delegate:self pushPhotoPickerVc:YES];
if (self.selectMaxImageCount >1) {
imagePicker.selectedAssets = self.selectedAssets;// 目前已经选中的图片数组
}
// 是否显示可选原图按钮
imagePicker.allowPickingOriginalPhoto = NO;
// 是否允许显示视频
imagePicker.allowPickingVideo = NO;
// 是否允许显示图片
imagePicker.allowPickingImage = YES;
// 设置是否显示图片序号
imagePicker.showSelectedIndex = YES;
// 你可以通过block或者代理,来得到用户选择的照片.
// [imagePicker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
//
// }];
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
选择图片代理方法
// 这个照片选择器会自己dismiss,当选择器dismiss的时候,会执行下面的代理方法
// 你也可以设置autoDismiss属性为NO,选择器就不会自己dismis了
// 如果isSelectOriginalPhoto为YES,表明用户选择了原图
// 你可以通过一个asset获得原图,通过这个方法:[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]
// photos数组里的UIImage对象,默认是828像素宽,你可以通过设置photoWidth属性的值来改变它
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos{
[self.selectedPhotos addObjectsFromArray:photos];
[self.selectedAssets addObjectsFromArray:assets];
NSLog(@"====photos:%lu",(unsigned long)self.selectedPhotos.count);
self.headImage.image = self.selectedPhotos.lastObject;
}
选择视频
//MaxImagesCount 可以选着的最大条目数
TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:2 delegate:self];
// 是否显示可选原图按钮
imagePicker.allowPickingOriginalPhoto = NO;
// 是否允许显示视频
imagePicker.allowPickingVideo = YES;
// 是否允许显示图片
imagePicker.allowPickingImage = NO;
// 这是一个navigation 只能present
// 设置 模态弹出模式。 iOS 13默认非全屏
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
选择视频代理方法
// 选择视频回调
// 如果用户选择了一个视频且allowPickingMultipleVideo是NO,下面的代理方法会被执行
// 如果allowPickingMultipleVideo是YES,将会调用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset{
}
2.重要属性
2.1 TZImage内部可以拍摄视频和图片 (尽量放在里面做)
allowTakePicture = YES;
allowTakeVideo = YES;
2.2 选择图片或视频排序
sortAscendingByModificationDate 升序 YES 基本都是升序
showSelectedIndex 显示选择序列号 YES 显示序列号
2.3 gif
allowPickingGif
NO: gif也显示 就是静止
YES: 就是可以动的图 且allowTakePicture 必须选择 不然没有图
2.4 选择视频和图片
allowPickingImage
allowPickingVideo
isSelectOriginalPhoto
allowPickingOriginalPhoto
1.选择图片和视频 任意一种或者2种 不能不选择 不然进去啥都没有了
2.选择图片可以配合 isSelectOriginalPhoto 是否选择原图
3.”单独“选择视频 isSelectOriginalPhoto 必须是NO YES没有意义了
2.5 允许多选视频/GIF/图片
//可以选择视频和图片
allowPickingMultipleVideo
2.6 isSelectOriginalPhoto
allowPickingOriginalPhoto 是否可以选择原图
isSelectOriginalPhoto 默认是不是原图 这里一般都是NO 默认也是NO
2.7 maxImagesCount 是1 单选模式- 裁剪
showSelectBtn = NO; //单选模式才可以NO 而且只针对单选模式有效 就是隐藏
裁剪
allowCrop = YES; //YES和NO不影响选择视频 但是视频也不能裁剪
needCircleCrop //是否是圆形裁剪 YES 则是圆形裁剪 NO 方形
//设置竖屏幕下裁剪尺寸
cropRect 大小 自己设置
scaleAspectFillCrop == YES;
//设置横屏幕下裁剪尺寸
横屏可以不设置 默认是竖屏下的
2.8 默认已经选择的数组
PHAsset *asset
selectedAssets 属性就是可变数组 可变数组放入的PHAsset *类型
2.9 拍摄时间设置
videoMaximumDuration = 10;//可以录制10s视频
视频拍摄质量videoQuality
[imagePickerVc setUiImagePickerControllerSettingBlock:^(UIImagePickerController *imagePickerController) {
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
}];
2.10 外观设置(一般默认)
//默认就行 那么下面就不需要写了 主题颜色可以设置成项目的
//imagePickerVc.navigationBar.barTintColor = [UIColor greenColor];
// imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
//imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
//imagePickerVc.navigationBar.translucent = NO;
//这些需要写
/// icon主题色,默认是微信的绿色,值是r:31 g:185 b:34。目前仅支持showSelectedIndex为YES时的图片选中icon。如需要,请尽早设置它。
imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
/// 默认是NO,如果设置为YES,当照片选择张数达到maxImagesCount时,其它照片会显示颜色为cannotSelectLayerColor的浮层
showPhotoCannotSelectLayer = YES;
//这就是超出数量的浮层
imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
//设置属性的。比如doneButton 设置红色字体 就是完成字体改成红色
[imagePickerVc setPhotoPickerPageUIConfigBlock:^(UICollectionView *collectionView, UIView *bottomToolBar, UIButton *previewButton, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel, UIView *divideLine) {
[doneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}];
还有自定义导航栏的返回按钮
....很多可以改的 具体看demo和.h
2.11 statusBarStyle(很少用,默认就行)
// imagePickerVc.isStatusBarDefault = NO;
// imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
2.12 自定义gif播放方案
隐藏与否 选择gif 都会播放 暂时复制下来 以后再看看
[[TZImagePickerConfig sharedInstance] setGifImagePlayBlock:^(TZPhotoPreviewView *view, UIImageView *imageView, NSData *gifData, NSDictionary *info) {
FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:gifData];
FLAnimatedImageView *animatedImageView;
for (UIView *subview in imageView.subviews) {
if ([subview isKindOfClass:[FLAnimatedImageView class]]) {
animatedImageView = (FLAnimatedImageView *)subview;
animatedImageView.frame = imageView.bounds;
animatedImageView.animatedImage = nil;
}
}
if (!animatedImageView) {
animatedImageView = [[FLAnimatedImageView alloc] initWithFrame:imageView.bounds];
animatedImageView.runLoopMode = NSDefaultRunLoopMode;
[imageView addSubview:animatedImageView];
}
animatedImageView.animatedImage = animatedImage;
}];
3.代理方法回调
3.1 用户取消
/// 用户点击了取消
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// NSLog(@"cancel");
}
3.2 asset显示与否 对视频 图片大小做限制的显示(也可以不写)
// 决定照片显示与否
- (BOOL)isAssetCanSelect:(PHAsset *)asset {
switch (asset.mediaType) {
case PHAssetMediaTypeVideo: {
// 视频时长
NSTimeInterval duration = asset.duration;
if (duration < 1) {
return NO;
}
return YES;
} break;
case PHAssetMediaTypeImage: {
// 图片尺寸
if (asset.pixelWidth > 3000 || asset.pixelHeight > 3000) {
// return NO;
}
return YES;
} break;
case PHAssetMediaTypeAudio:
return NO;
break;
case PHAssetMediaTypeUnknown:
return NO;
break;
default: break;
}
return YES;
}
3.3 相册是否显示
// 决定相册显示与否 albumName:相册名字 result:相册原始数据
- (BOOL)isAlbumCanSelect:(NSString *)albumName result:(PHFetchResult *)result {
/*
if ([albumName isEqualToString:@"个人收藏"]) {
return NO;
}
if ([albumName isEqualToString:@"视频"]) {
return NO;
}*/
return YES;
}
3.4 图片回调
/ 这个照片选择器会自己dismiss,当选择器dismiss的时候,会执行下面的代理方法
// 你也可以设置autoDismiss属性为NO,选择器就不会自己dismis了
// 如果isSelectOriginalPhoto为YES,表明用户选择了原图
// 你可以通过一个asset获得原图,通过这个方法:[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]
// photos数组里的UIImage对象,默认是828像素宽,你可以通过设置photoWidth属性的值来改变它
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos{
NSLog(@"图片回调");
[self.selectedPhotos addObjectsFromArray:photos];
[self.selectedAssets addObjectsFromArray:assets];
NSLog(@"====photos:%lu",(unsigned long)self.selectedPhotos.count);
//刷新数据
......
}
3.5 视频回调
// 选择视频回调
// 如果用户选择了一个视频且allowPickingMultipleVideo是NO,下面的代理方法会被执行
// 如果allowPickingMultipleVideo是YES,将会调用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset{
NSLog(@"视频回调");
[self.selectedPhotos addObjectsFromArray: @[coverImage]];
[self.selectedAssets addObjectsFromArray:@[asset]];
// open this code to send video / 打开这段代码发送视频
[[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPresetLowQuality success:^(NSString *outputPath) {
// NSData *data = [NSData dataWithContentsOfFile:outputPath];
NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
// Export completed, send video here, send by outputPath or NSData
// 导出完成,在这里写上传代码,通过路径或者通过NSData上传
} failure:^(NSString *errorMessage, NSError *error) {
NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
}];
//刷新数据
......
}
3.6 GIF回调
// 选择gif图片回调
// 如果用户选择了一个gif图片且allowPickingMultipleVideo是NO,下面的代理方法会被执行
// 如果allowPickingMultipleVideo是YES,将会调用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
//1.如果allowPickingMultipleVideo 是YES allowPickingGif 是YES 那么都不会走视频的回调 gif回调 全部都是图片回调 具体显示cell 还是需要判断asset
//2.判断是视频asset.mediaType != PHAssetMediaTypeVideo
//3.判断是gif [[asset valueForKey:@"filename"] containsString:@"GIF"]
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(PHAsset *)asset{
NSLog(@"git回调");
[self.selectedPhotos addObjectsFromArray: @[animatedImage]];
[self.selectedAssets addObjectsFromArray:@[asset]];
}
3.7 查看已选择的图片
//单独查看一张gif图
TZGifPhotoPreviewController *vc = [[TZGifPhotoPreviewController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhotoGif timeLength:@""];
vc.model = model;
[self presentViewController:vc animated:YES completion:nil];
//单独查看一个视频
TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
vc.model = model;
[self presentViewController:vc animated:YES completion:nil];
//这是最全的 首先 可以多选视频/gif /图片 然后可以传入是否原图 然后点击取消 点击选择 然后完成 就可以回调最新的数据源
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.item];
//这个参数大致传入就可以
imagePickerVc.maxImagesCount = self.maxCountTF.text.integerValue;
//是否可以gif
imagePickerVc.allowPickingGif = YES;
//是否可以原图
imagePickerVc.allowPickingOriginalPhoto = YES;
//是否可以展示视频
imagePickerVc.allowPickingMultipleVideo = YES;
//是否显示下标
imagePickerVc.showSelectedIndex = self.showSelectedIndexSwitch.
//是否默认就选择原图
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
NSLog(@"回调:%ld,%ld",photos.count,assets.count);
}];
[self presentViewController:imagePickerVc animated:YES completion:nil];