从相册里获取所有的视频并展示
需要引入#import <Photos/Photos.h> 这个库
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult*assetsFetchResults = [PHAssetfetchAssetsWithOptions:options];
__block NSInteger count = assetsFetchResults.count;
for(NSIntegeri =0; i < assetsFetchResults.count; i++) {
PHAsset*phAsset = assetsFetchResults[i];
//获取图片后处理
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
PHCachingImageManager *manager = [[PHCachingImageManager alloc]init];
[manager requestPlayerItemForVideo:phAssetoptions:optionsresultHandler:^(AVPlayerItem*_NullableplayerItem,NSDictionary*_Nullableinfo) {
AVURLAsset*urlAsset = (AVURLAsset*)playerItem.asset;
CMTime timeStr = [playerItem.asset duration];
//url
NSURL*url = urlAsset.URL;
if(url !=nil) {
NSIntegerdTotalSeconds =CMTimeGetSeconds(timeStr);
NSIntegerdHours =floor(dTotalSeconds /3600);
NSIntegerdMinutes =floor(dTotalSeconds %3600/60);
NSIntegerdSeconds =floor(dTotalSeconds %3600%60);
//时长
NSString*videoDurationText = [NSString stringWithFormat:@"%ld:%ld:%ld",dHours, dMinutes, dSeconds];
NSDictionary*dic =@{@"time":videoDurationText,@"url":url};
[self.videoArraddObject:dic];
}else{
count = count-1;
}
if(count ==self.videoArr.count) {
NSLog(@"videoArr.count == %ld",self.videoArr.count);
[self.collectionViewreloadData];
}
}];
}
网友评论