相机、相册iOSIOS知识积累

TZImagePickerController用法

2019-07-03  本文已影响0人  守护地中海的花

功能十分强大 最新版本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内部可以拍摄视频和图片 所以尽量放在里面做

allowTakePicture  = YES;
allowTakeVideo = YES;

2.无关紧要的参数

sortAscendingByModificationDate 升序 YES 基本都是升序
showSelectedIndex 显示选择序列号  YES 显示序列号

3.gif

allowPickingGif 
NO: gif也显示 就是静止  
YES: 就是可以动的图 且allowTakePicture 必须选择 不然没有图

4.选择视频和图片

allowPickingImage
allowPickingVideo
isSelectOriginalPhoto
allowPickingOriginalPhoto
1.选择图片和视频 任意一种或者2种 不能不选择 不然进去啥都没有了
2.选择图片可以配合 isSelectOriginalPhoto 是否选择原图
3.”单独“选择视频  isSelectOriginalPhoto 必须是NO YES没有意义了 

5.允许多选视频/GIF/图片

allowPickingMultipleVideo
1.即可以选择视频和图片

6.isSelectOriginalPhoto

allowPickingOriginalPhoto 是否可以选择原图
isSelectOriginalPhoto 默认是不是原图  这里一般都是NO 默认也是NO
原图 如果选择一个 默认都是选择的

7.maxImagesCount 是1 单选模式-> 裁剪

showSelectBtn = NO; //单选模式才可以NO 而且只针对单选模式有效 就是隐藏

裁剪
allowCrop = YES; //YES和NO不影响选择视频 但是视频也不能裁剪
needCircleCrop //是否是圆形裁剪 YES 则是圆形裁剪 NO 方形
//设置竖屏幕下裁剪尺寸
cropRect 大小 自己设置
scaleAspectFillCrop == YES;
//设置横屏幕下裁剪尺寸
横屏可以不设置 默认是竖屏下的 具体看Demo

8.带入默认已经选择的数组 这样不会重复了

PHAsset *asset
selectedAssets 属性就是可变数组 可变数组放入的PHAsset *类型 

9.拍摄时间设置

videoMaximumDuration = 10;//可以录制10s视频
视频拍摄质量videoQuality
[imagePickerVc setUiImagePickerControllerSettingBlock:^(UIImagePickerController *imagePickerController) {
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
    }];

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

11.图片最低选择(很少用)

minImagesCount 默认是0 如果 3 低于3张照片点击完成会弹出弹出框提示

12.完成按钮点击(很少用)

alwaysEnableDoneBtn = YES 不选择任何一张图片/视频都可以点击完成 退出TZ 

13.图片选择规格 (很少用)

minPhotoWidthSelectable = 3000
minPhotoHeightSelectable = 2000 说明图片宽或者高小于 3000 20000 就不能选择 进入TZ之后没有选择按钮 即使进入图片浏览也是不能选择的

14.预览图片(很少用)

allowPreview 预览图片 默认YES 可以预览 如果NO 则点击直接选择

15.statusBarStyle (很少用 不写默认)

//     imagePickerVc.isStatusBarDefault = NO;
//    imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;

16.自定义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;
    }];

17.语言 (默认即可)

// 设置首选语言 / Set preferred language
    // imagePickerVc.preferredLanguage = @"zh-Hans";
    
    // 设置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) {

    }];

完美分割线 下面是 代理方法 回调


<TZImagePickerControllerDelegate>
#pragma mark - TZImagePickerControllerDelegate

1.用户取消

/// 用户点击了取消
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
    // NSLog(@"cancel");
}

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.// 决定相册显示与否

- (BOOL)isAlbumCanSelect:(NSString *)albumName result:(PHFetchResult *)result {
    /*
    if ([albumName isEqualToString:@"个人收藏"]) {
        return NO;
    }
    if ([albumName isEqualToString:@"视频"]) {
        return NO;
    }*/
    return YES;
}

4.图片回调+git+视频

// 这个照片选择器会自己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 {


}
1.如果allowPickingMultipleVideo 是YES allowPickingGif 是YES 那么都不会走视频的回调 gif回调 全部都是图片回调 具体显示cell 还是需要判断asset
2.判断是视频asset.mediaType != PHAssetMediaTypeVideo
3.判断是gif [[asset valueForKey:@"filename"] containsString:@"GIF"]

// 如果用户选择了一个gif图片且allowPickingMultipleVideo是NO,下面的代理方法会被执行
// 如果allowPickingMultipleVideo是YES,将会调用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(PHAsset *)asset {
    NSLog(@"git回调333");
    _selectedPhotos = [NSMutableArray arrayWithArray:@[animatedImage]];
    _selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
    [_collectionView reloadData];
}

// 如果用户选择了一个视频且allowPickingMultipleVideo是NO,下面的代理方法会被执行
// 如果allowPickingMultipleVideo是YES,将会调用imagePickerController:didFinishPickingPhotos:sourceAssets:isSelectOriginalPhoto:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset {
    NSLog(@"视频回调2222");
    _selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
    _selectedAssets = [NSMutableArray arrayWithArray:@[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);
    }];
    [_collectionView reloadData];
    // _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
}

完美分割线 下面是 查看已选择的图片


//单独查看一张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];

完美分割线 下面是 图片上传


上一篇 下一篇

猜你喜欢

热点阅读