第一种
UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *video = [NSData dataWithContentsOfURL:url];
if (video.length/1024/1024 >100) { // video size is more than 100 M
// code goes here
return;
}
NSURL *videoSaveURL = kVideoSavePath;
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = videoSaveURL;
exportSession.outputFileType = AVFileTypeMPEG4; // mp4
[exportSession exportAsynchronouslyWithCompletionHandler:^{
int exportState = exportSession.status;
switch (exportState) {
case AVAssetExportSessionStatusFailed: // export failed
// code goes here
break;
case AVAssetExportSessionStatusCompleted: // finish
{
// first frame of video
#if 1
AVAssetImageGenerator *imgeGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
imgeGenerator.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSValue* timeValue = [NSValue valueWithCMTime:time];
[imgeGenerator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
// image
UIImage *newImage = [UIImage imageWithCGImage:image];
// video in sandbox at 'kVideoSavePath'
// code...
}];
#else
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError *error;
CGImageRef imageRef = [generator copyCGImageAtTime:CMTimeMake(0, 10) actualTime:NULL error:&error];
if (error) {
return nil;
}
UIImage *image = [UIImage imageWithCGImage:imageRef];
#endif
}
break;
default:
break;
}
}];
}
第二种
PHAsset
// Option
PHVideoRequestOptions *option = [[PHVideoRequestOptions alloc] init];
option.version = PHVideoRequestOptionsVersionCurrent; // default
option.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic; // default
// Manager
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestExportSessionForVideo:asset options:option exportPreset:AVAssetExportPresetMediumQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
// Path
NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"saved.mp4"];
NSLog(@"videoPath:%@",videoPath);
// Export
exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
exportSession.shouldOptimizeForNetworkUse = NO;
exportSession.outputFileType = AVFileTypeMPEG4; // mp4
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:{
// code...
}break;
case AVAssetExportSessionStatusCompleted:{
// code...
}break;
default:
break;
}
}];
}];
网友评论