我們在上傳視頻的時候需要上傳視頻的第一幀渊迁,這時候就需要拿到視頻的本地路徑
1、利用TZImageManager 導(dǎo)出圖片
- (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(@"視頻導(dǎo)出到本地完成,沙盒路徑為:%@",outputPath);
// Export completed, send video here, send by outputPath or NSData
// 導(dǎo)出完成,在這里寫上傳代碼座每,通過路徑或者通過NSData上傳
//拿到data 開始回調(diào)
[weakSelf requestDataBlock:data andOutPutPath:outputPath];
} failure:^(NSString *errorMessage, NSError *error) {
NSLog(@"視頻導(dǎo)出失敗:%@,error:%@",errorMessage, error);
}];
}
}
2、根據(jù)路徑獲取圖片
//視頻,根據(jù)路徑獲取圖片
UIImage *img = [self getVideoPreViewImage:[NSURL URLWithString:videoUrlStr]];
3摘悴、獲取圖片的方法
//根據(jù)本地路徑獲取視頻的第一幀
- (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.