iOS 获取视频缩略图
首先导入AVFoundation库
UIImagePickerController
- 调用摄像头拍照,摄像的时候会用到UIImagePickerController
// 实例化一个对象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// 设置图片的类型
imagePicker.sourceType = [UIImagePickerControllerSourceTypeCamera];
// 设置媒体的种类
imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
// 允许编辑
imagePicker.allowsEditing = NO;
// 闪灯光模式
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
// 摄像的前置或者后置
imagePicker.cameraDevice = UIImagePickerControllerCameraRear;
imagePicker.delegate = self;
// 通过模态视图推出视图页面
[self presentViewController:imagePicker animated:YES completion:nil];
注释:参数说明
sourceType 的三种类型分别是:
- UIImagePickerControllerSourceTypeCamera 照相机
- UIImagePickerControllerSourceTypePhotoLibrary 照片库 (用户不可删除)
- UIImagePickerControllerSourceTypeSavedPhotosAlbum 通过摄像头保存的照片(用户可删除)
实现PickerController的代理属性
- 需要给它设置代理,然后遵守两个协议 ,实现下面的代理方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
- 通常情况下,在didFinish方法里面对照片或视频进行保存处理;在didCancel方法里dismissViewController
获取视频缩略图
- (UIImage *)getScreenShotImageFromVideoPath:(NSString *)filePath{
UIImage *shotImage;
//视频路径URL
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL 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];
shotImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return shotImage;
}
网友评论