iOS 根据本地路径获取视频的第一帧

我们在上传视频的时候需要上传视频的第一帧,这时候就需要拿到视频的本地路径

1、利用TZImageManager 导出图片

- (void)refreshCollectionViewWithAddedAsset:(PHAsset *)asset image:(UIImage *)image {
    [_selectedAssets addObject:asset];
    [_selectedPhotos addObject:image];
    
    if ([asset isKindOfClass:[PHAsset class]]) {
        PHAsset *phAsset = asset;
        NSLog(@"location:%@",phAsset.location);
        WEAKSELF;
        [[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPreset640x480 success:^(NSString *outputPath) {
            NSData *data = [NSData dataWithContentsOfFile:outputPath];
            NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
            // Export completed, send video here, send by outputPath or NSData
            // 导出完成,在这里写上传代码,通过路径或者通过NSData上传
            //拿到data 开始回调
            [weakSelf requestDataBlock:data andOutPutPath:outputPath];
        } failure:^(NSString *errorMessage, NSError *error) {
            NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
        }];
    }
}

2、根据路径获取图片

//视频,根据路径获取图片
        UIImage *img = [self getVideoPreViewImage:[NSURL URLWithString:videoUrlStr]];

3、获取图片的方法

//根据本地路径获取视频的第一帧
- (UIImage*)getVideoPreViewImage:(NSURL *)url
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *img = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return img;
}

喜欢点个赞!

END.

你可能感兴趣的:(iOS 根据本地路径获取视频的第一帧)