功能十分強(qiáng)大 最新版本3.2.1 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"
1.TZImage內(nèi)部可以拍攝視頻和圖片 所以盡量放在里面做
allowTakePicture = YES;
allowTakeVideo = YES;
2.無關(guān)緊要的參數(shù)
sortAscendingByModificationDate 升序 YES 基本都是升序
showSelectedIndex 顯示選擇序列號(hào) YES 顯示序列號(hào)
3.gif
allowPickingGif
NO: gif也顯示 就是靜止
YES: 就是可以動(dòng)的圖 且allowTakePicture 必須選擇 不然沒有圖
4.選擇視頻和圖片
allowPickingImage
allowPickingVideo
isSelectOriginalPhoto
allowPickingOriginalPhoto
1.選擇圖片和視頻 任意一種或者2種 不能不選擇 不然進(jìn)去啥都沒有了
2.選擇圖片可以配合 isSelectOriginalPhoto 是否選擇原圖
3.”單獨(dú)“選擇視頻 isSelectOriginalPhoto 必須是NO YES沒有意義了
5.允許多選視頻/GIF/圖片
allowPickingMultipleVideo
1.即可以選擇視頻和圖片
6.isSelectOriginalPhoto
allowPickingOriginalPhoto 是否可以選擇原圖
isSelectOriginalPhoto 默認(rèn)是不是原圖 這里一般都是NO 默認(rèn)也是NO
原圖 如果選擇一個(gè) 默認(rèn)都是選擇的
7.maxImagesCount 是1 單選模式-> 裁剪
showSelectBtn = NO; //單選模式才可以NO 而且只針對(duì)單選模式有效 就是隱藏
裁剪
allowCrop = YES; //YES和NO不影響選擇視頻 但是視頻也不能裁剪
needCircleCrop //是否是圓形裁剪 YES 則是圓形裁剪 NO 方形
//設(shè)置豎屏幕下裁剪尺寸
cropRect 大小 自己設(shè)置
scaleAspectFillCrop == YES;
//設(shè)置橫屏幕下裁剪尺寸
橫屏可以不設(shè)置 默認(rèn)是豎屏下的 具體看Demo
8.帶入默認(rèn)已經(jīng)選擇的數(shù)組 這樣不會(huì)重復(fù)了
PHAsset *asset
selectedAssets 屬性就是可變數(shù)組 可變數(shù)組放入的PHAsset *類型
9.拍攝時(shí)間設(shè)置
videoMaximumDuration = 10;//可以錄制10s視頻
視頻拍攝質(zhì)量videoQuality
[imagePickerVc setUiImagePickerControllerSettingBlock:^(UIImagePickerController *imagePickerController) {
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
}];
10.外觀 默認(rèn)就行 不需要單獨(dú)寫 就是默認(rèn)的
//默認(rèn)就行 那么下面就不需要寫了 主題顏色可以設(shè)置成項(xiàng)目的
//imagePickerVc.navigationBar.barTintColor = [UIColor greenColor];
// imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
//imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
//imagePickerVc.navigationBar.translucent = NO;
//這些需要寫一哈
/// icon主題色,默認(rèn)是微信的綠色久脯,值是r:31 g:185 b:34。目前僅支持showSelectedIndex為YES時(shí)的圖片選中icon跑慕。如需要骡和,請(qǐng)盡早設(shè)置它慰于。
imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
/// 默認(rèn)是NO唤衫,如果設(shè)置為YES佳励,當(dāng)照片選擇張數(shù)達(dá)到maxImagesCount時(shí),其它照片會(huì)顯示顏色為cannotSelectLayerColor的浮層
showPhotoCannotSelectLayer = YES;
//這就是超出數(shù)量的浮層
imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
//設(shè)置屬性的妙黍。比如doneButton 設(shè)置紅色字體 就是完成字體改成紅色
[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];
}];
還有自定義導(dǎo)航欄的返回按鈕
....很多可以改的 具體看demo和.h
11.圖片最低選擇(很少用)
minImagesCount 默認(rèn)是0 如果 3 低于3張照片點(diǎn)擊完成會(huì)彈出彈出框提示
12.完成按鈕點(diǎn)擊(很少用)
alwaysEnableDoneBtn = YES 不選擇任何一張圖片/視頻都可以點(diǎn)擊完成 退出TZ
13.圖片選擇規(guī)格 (很少用)
minPhotoWidthSelectable = 3000
minPhotoHeightSelectable = 2000 說明圖片寬或者高小于 3000 20000 就不能選擇 進(jìn)入TZ之后沒有選擇按鈕 即使進(jìn)入圖片瀏覽也是不能選擇的
14.預(yù)覽圖片(很少用)
allowPreview 預(yù)覽圖片 默認(rèn)YES 可以預(yù)覽 如果NO 則點(diǎn)擊直接選擇
15.statusBarStyle (很少用 不寫默認(rèn))
// imagePickerVc.isStatusBarDefault = NO;
// imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
16.自定義gif播放方案
隱藏與否 選擇gif 都會(huì)播放 暫時(shí)復(fù)制下來 以后再看看
[[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;
}];
17.語言 (默認(rèn)即可)
// 設(shè)置首選語言 / Set preferred language
// imagePickerVc.preferredLanguage = @"zh-Hans";
// 設(shè)置languageBundle以使用其它語言 / Set languageBundle to use other language
// imagePickerVc.languageBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"tz-ru" ofType:@"lproj"]];
18.返回block 最好用代理
// You can get the photos by block, the same as by delegate.
// 你可以通過block或者代理抓于,來得到用戶選擇的照片.
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
}];
完美分割線 下面是 代理方法 回調(diào)
<TZImagePickerControllerDelegate>
#pragma mark - TZImagePickerControllerDelegate
1.用戶取消
/// 用戶點(diǎn)擊了取消
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// NSLog(@"cancel");
}
2.// 決定asset顯示與否 對(duì)視頻 圖片大小做限制的顯示(也可以不寫)
- (BOOL)isAssetCanSelect:(PHAsset *)asset {
switch (asset.mediaType) {
case PHAssetMediaTypeVideo: {
// 視頻時(shí)長
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.// 決定相冊(cè)顯示與否
- (BOOL)isAlbumCanSelect:(NSString *)albumName result:(PHFetchResult *)result {
/*
if ([albumName isEqualToString:@"個(gè)人收藏"]) {
return NO;
}
if ([albumName isEqualToString:@"視頻"]) {
return NO;
}*/
return YES;
}
4.圖片回調(diào)+git+視頻
// 這個(gè)照片選擇器會(huì)自己dismiss怕品,當(dāng)選擇器dismiss的時(shí)候巾遭,會(huì)執(zhí)行下面的代理方法
// 你也可以設(shè)置autoDismiss屬性為NO灼舍,選擇器就不會(huì)自己dismis了
// 如果isSelectOriginalPhoto為YES,表明用戶選擇了原圖
// 你可以通過一個(gè)asset獲得原圖纹安,通過這個(gè)方法:[[TZImageManager manager] getOriginalPhotoWithAsset:completion:]
// photos數(shù)組里的UIImage對(duì)象,默認(rèn)是828像素寬光督,你可以通過設(shè)置photoWidth屬性的值來改變它
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
}
1.如果allowPickingMultipleVideo 是YES allowPickingGif 是YES 那么都不會(huì)走視頻的回調(diào) gif回調(diào) 全部都是圖片回調(diào) 具體顯示cell 還是需要判斷asset
2.判斷是視頻asset.mediaType != PHAssetMediaTypeVideo
3.判斷是gif [[asset valueForKey:@"filename"] containsString:@"GIF"]
// 如果用戶選擇了一個(gè)gif圖片且allowPickingMultipleVideo是NO结借,下面的代理方法會(huì)被執(zhí)行
// 如果allowPickingMultipleVideo是YES卒茬,將會(huì)調(diào)用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(PHAsset *)asset {
NSLog(@"git回調(diào)333");
_selectedPhotos = [NSMutableArray arrayWithArray:@[animatedImage]];
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
[_collectionView reloadData];
}
// 如果用戶選擇了一個(gè)視頻且allowPickingMultipleVideo是NO圃酵,下面的代理方法會(huì)被執(zhí)行
// 如果allowPickingMultipleVideo是YES,將會(huì)調(diào)用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset {
NSLog(@"視頻回調(diào)2222");
_selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
// open this code to send video / 打開這段代碼發(fā)送視頻
[[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPresetLowQuality success:^(NSString *outputPath) {
// NSData *data = [NSData dataWithContentsOfFile:outputPath];
NSLog(@"視頻導(dǎo)出到本地完成,沙盒路徑為:%@",outputPath);
// Export completed, send video here, send by outputPath or NSData
// 導(dǎo)出完成,在這里寫上傳代碼捌锭,通過路徑或者通過NSData上傳
} failure:^(NSString *errorMessage, NSError *error) {
NSLog(@"視頻導(dǎo)出失敗:%@,error:%@",errorMessage, error);
}];
[_collectionView reloadData];
// _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
}
完美分割線 下面是 查看已選擇的圖片
//單獨(dú)查看一張gif圖
TZGifPhotoPreviewController *vc = [[TZGifPhotoPreviewController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypePhotoGif timeLength:@""];
vc.model = model;
[self presentViewController:vc animated:YES completion:nil];
//單獨(dú)查看一個(gè)視頻
TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
vc.model = model;
[self presentViewController:vc animated:YES completion:nil];
//這是最牛逼的 首先 可以多選視頻/gif /圖片 然后可以傳入是否原圖 然后點(diǎn)擊取消 點(diǎn)擊選擇 然后完成 就可以回調(diào)最新的數(shù)據(jù)源
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.item];
//這個(gè)參數(shù)大致傳入就可以
imagePickerVc.maxImagesCount = self.maxCountTF.text.integerValue;
//是否可以gif
imagePickerVc.allowPickingGif = YES;
//是否可以原圖
imagePickerVc.allowPickingOriginalPhoto = YES;
//是否可以展示視頻
imagePickerVc.allowPickingMultipleVideo = YES;
//是否顯示下標(biāo)
imagePickerVc.showSelectedIndex = self.showSelectedIndexSwitch.
//是否默認(rèn)就選擇原圖
imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
NSLog(@"預(yù)覽回調(diào):%ld,%ld",photos.count,assets.count);
}];
[self presentViewController:imagePickerVc animated:YES completion:nil];