- (void)getVideoData{
// 获取所有资源的集合,并按资源的创建时间排序
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:options];for (NSInteger i = 0; i < assetsFetchResults.count; i++) {
PHAsset *asset = assetsFetchResults[i];
//异步执行
[[PHImageManager defaultManager]requestPlayerItemForVideo:asset options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
[self.VideoArr addObject:playerItem];
}];
//资源转图片
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
//原图
UIImage *image = [UIImage imageWithData:imageData];
//缩略图
UIImage *ima = [self thumbnailWithImage:image size:CGSizeMake(60, 60)];
//获取到视频封面图片放入数组(原图或缩略图自己选择吧)
//主线刷新列表
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}];
});
}
}
#pragma mark --- 获取所有本地相册图片资源
方法跟获取视频那个方法一样,就是有个属性直接可以获取(如蓝色字体部分)
#pragma mark --- 获取所有图片资源
- (void)getPhotoData{
// 获取所有资源的集合,并按资源的创建时间排序
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:options];
for (NSInteger i = 0; i < assetsFetchResults.count; i++) {
PHAsset *asset = assetsFetchResults[i];
dispatch_async(dispatch_queue_create("com.tany.searchMarkDb", DISPATCH_QUEUE_SERIAL), ^{
// 异步操作
//资源转图片
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage * image = [UIImage imageWithData:imageData];
//缩略图
UIImage *ima = [self thumbnailWithImage:image size:CGSizeMake(60, 60)];
[self.arr addObject:ima];
[self.OrigArr addObject:image];
// if (self.arr.count == assetsFetchResults.count){
dispatch_async(dispatch_get_main_queue(), ^{
[self.CollectionView reloadData];
});
// }
}];
});
}
}
#pragma mark - 调用本地摄像机,录制视频
//调起摄相机
-(void)camera{
UIImagePickerController * PhotoCamera =[[UIImagePickerController alloc]init];
PhotoCamera.delegate =self;
PhotoCamera.allowsEditing = YES;
PhotoCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
PhotoCamera.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
PhotoCamera.videoQuality = UIImagePickerControllerQualityTypeLow;
[self presentViewController:PhotoCamera animated:YES completion:nil];
}
#pragma mark - 代理回调
#pragma mark - UIImagePickerController Delegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//获取视频路径
NSURL *Url = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *videoPath = [Url path];
//视频资源
NSData *videoData = [NSData dataWithContentsOfFile:videoPath];
NSLog(@"%@",videoPath);
//获取该视频的封面图
UIImage *image = [self getThumbnailImage:videoPath];
[self dismissViewControllerAnimated:NO completion:nil];
}
//获取该视频的封面图
-(UIImage *)getThumbnailImage:(NSString *)videoPath {
if (videoPath) {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath: videoPath] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
// 设定缩略图的方向
// 如果不设定,可能会在视频旋转90/180/270°时,获取到的缩略图是被旋转过的,而不是正向的
gen.appliesPreferredTrackTransform = YES;
// 设置图片的最大size(分辨率)
gen.maximumSize = CGSizeMake(300, 169);
CMTime time = CMTimeMakeWithSeconds(0.0, 600); //取第0秒,一秒钟600帧
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
if (error) {
UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
return placeHoldImg;
}
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
} else {
UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
return placeHoldImg;
}
}
//把图片缩放到指定大小
- (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
UIGraphicsBeginImageContext(asize);
[image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
(PS:demo就不给了,学会自己动手