登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

ios 上传视频选择视频封面(videoCoverController)

[复制链接]
来自: 你们纯洁哥 分类: iOS精品源码 上传时间: 2017-12-9 10:59:28
Tag:

项目介绍:

起初看到这个功能我是拒绝的,之前做的视频上传都是获取特定的帧数当封面,没有刻意的去选择封面,但是需求已定,随后网上也找了下,没有类似的,于是乎就自己写了一个,有什么改进的地方可以互相交流,话不多说直接上代码了

1.打开相册,系统相册用的很顺手,所以一直就用系统的相册

//两个代理
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;

@end
//代理方法
//打开相册
- (void)openImagePickerController:(UIImagePickerControllerSourceType)type
{
    if (![UIImagePickerController isSourceTypeAvailable:type]) return;
    
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    
    //视频最大时间
    ipc.videoMaximumDuration = 30;
    ipc.view.backgroundColor = [UIColor whiteColor];
    ipc.sourceType = type;
    ipc.delegate = self;
    //只打开视频
    ipc.mediaTypes = @[(NSString *)kUTTypeMovie];
    //视频上传质量
    ipc.videoQuality = UIImagePickerControllerQualityTypeHigh;
    [self presentViewController:ipc animated:YES completion:nil];
}


#pragma mark - UIImagePickerControllerDelegate
/**
 * 从UIImagePickerController选择完图片后就调用(拍照完毕或者选择相册图片完毕)
 */

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    CZHWeakSelf(self);
   
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        //如果是视频
        NSURL *url = info[UIImagePickerControllerMediaURL];
        //计算相册视频时长
        NSDictionary *videoDic = [CZHTool getLocalVideoSizeAndTimeWithSourcePath:url.absoluteString];
        int videoTime = [[videoDic valueForKey:@"duration"] intValue];
      //视频限制的长度
        NSUInteger limitTime = 30;
        if (videoTime > limitTime) {
            [picker dismissViewControllerAnimated:YES completion:nil];
            return;
        }
        //把系统相册mov格式转换成mp4格式
        [CZHTool convertMovTypeIntoMp4TypeWithSourceUrl:url convertSuccess:^(NSURL *path) {
           CZHStrongSelf(self);
           [picker dismissViewControllerAnimated:YES completion:nil];
            //选择封面控制器
           CZHChooseCoverController *chooseCover = [[CZHChooseCoverController alloc] init];
            //本地路径
           chooseCover.videoPath = path;
            //选择封面的block,把封面image回调
           chooseCover.coverImageBlock = ^(UIImage *coverImage) {
                self.coverImageView.image = coverImage;
                
                //上传视频操作
                //[self changeWithUploadSource:path];
                //上传封面操作
                //[self uploadCoverWithImage:coverImage];
            };
            [self presentViewController:chooseCover animated:YES completion:nil];
        }];
    }
}

// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    // dismiss UIImagePickerController
    [self dismissViewControllerAnimated:YES completion:nil];
}

2.选择封面控制器

//截取几张图片放在底部用作展示,我是用collectionview做展示
  AVURLAsset * asset = [AVURLAsset assetWithURL:self.videoPath];
    CMTime  time = [asset duration];
    self.timeValue = time.value;
    self.timeScale = time.timescale;
    if (time.value < 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:self.videoPath options:opts];
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    generator.appliesPreferredTrackTransform = YES;
    //总帧数/展示的总数量
    long long baseCount = time.value / PHOTP_COUNT;
    //取出PHOTP_COUNT张图片,存放到数组,用于collectionview
    for (NSInteger i = 0 ; i < PHOTP_COUNT; i++) {
        
        NSError *error = nil;
        //每隔baseCount帧取一帧存起来,一共PHOTP_COUNT张
        CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(i * baseCount, time.timescale) actualTime:NULL error:&error];
        {
            UIImage *image = [UIImage imageWithCGImage:img];
            
            [self.photoArrays addObject:image];
        }
    }
//把存的存的几张图片用collectionview展示出来
CGRect collectionViewF = CGRectMake(0,  CZH_ScaleHeight(461), ScreenWidth, CZH_ScaleHeight(62.5));
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewF collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerClass:[CZHChooseCoverCell class] forCellWithReuseIdentifier:ID];
    [self.view addSubview:collectionView];
    self.collectionView  = collectionView;
    
    //在collectionview上面覆盖一个slider用于滑动选择图片
    UIImage *selected = [UIImage imageNamed:@"slider_select"];
    UIImage *deselected = [UIImage imageNamed:@"slider_deselect"];
    
    UISlider *slider = [[UISlider alloc] init];
    slider.frame = CGRectMake(0,  CZH_ScaleHeight(461), ScreenWidth, CZH_ScaleHeight(62.5));
    
    [slider setThumbImage:deselected forState:UIControlStateNormal];
    [slider setThumbImage:selected forState:UIControlStateHighlighted];
    //透明的图片
    UIImage *image = [CZHTool imageWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)];
    
    [slider setMinimumTrackImage:image forState:UIControlStateNormal];
    [slider setMaximumTrackImage:image forState:UIControlStateNormal];
    
    slider.maximumValue = self.timeValue;
    slider.minimumValue = 0;
    [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];

    //默认选取第一帧展示
    [self chooseWithTime:0];
    
//滑动slider的操作
- (void)valueChange:(UISlider *)sender {
    
    int timeValue = sender.value;
    
    [self chooseWithTime:timeValue];
}


- (void)chooseWithTime:(CMTimeValue)time {
    
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:self.videoPath options:opts];
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    generator.appliesPreferredTrackTransform = YES;
    //    generator.maximumSize = CGSizeMake(ScreenWidth, ScreenHeight);
    
    
    NSError *error = nil;
    CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(time, self.timeScale) actualTime:NULL error:&error];
    {
        UIImage *image = [UIImage imageWithCGImage:img];
        
        self.imageView.image = image;
    }
}
//点击返回按钮和完成按钮的操作
- (void)buttonClick:(UIButton *)sender {
    if (sender.tag == CZHChooseCoverControllerButtonTypeBack) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (sender.tag == CZHChooseCoverControllerButtonTypeComplete) {
        //封面图片回调
        if (_coverImageBlock) {
            _coverImageBlock(self.imageView.image);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

ps.模拟器相册没有视频的话可以用下面代码把项目的视频保存到相册

NSMutableArray *videoArray = [NSMutableArray array];
//工程中类型是MP4的文件数组
NSArray *movs = [[NSBundle mainBundle] pathsForResourcesOfType:@"mp4" inDirectory:nil];
[videoArray addObjectsFromArray:movs];
for (id item in videoArray) {
//循环保存到相册
  if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(item)) {
     UISaveVideoAtPathToSavedPhotosAlbum(item, self, nil, NULL);
    }
}

demo下载链接

查看博客
查看简书

相关源码推荐:

我来说两句
*滑动验证:
所有评论(14)
AlonMessi 2017-12-11 10:24:13
小弟学习了~支持,支持
回复
littleRed 2017-12-11 10:24:39
感谢分享,code4app有你更精彩
回复
kengsir 2017-12-11 10:24:41
好好 学习了 确实不错
回复
phoiu 2017-12-11 10:25:00
小弟学习了~支持,支持
回复
hellokenken 2017-12-11 10:25:17
感谢大神分享,必须顶!
回复
BlueManlove 2017-12-11 10:29:33
不错不错,楼主辛苦了。。。
回复
code4app热心网友 2017-12-11 13:11:59
楼主用心了,内容非常精彩。
回复
code4app热心网友 2017-12-11 13:40:59
楼主威武啊,Code4App有你更给力!
回复
code4app热心网友 2017-12-11 14:36:44
虽不明,但觉厉!
回复
12下一页
提取码:  下载次数:69 状态:已购或VIP 售价:0(原价:20)金钱 下载权限:初级码农 
1380 1 69
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表