- (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就不給了,學會自己動手