iOS9之后的扫描本地视频并播放

基于PhotoKit的扫描本地视频分类

最近的项目需要扫描本地的视频并播放,iOS9之后AL的已经过时,苹果推出了新的PhotoKit供大家对本地的图片和视频进行管理,于是把这块最长用的功能抽取成UIApplication的分类,方便快捷,直接返回数组的形式也方便调用,文章最后会附上demo的下载地址,供大家参考,如果有好的意见或建议欢迎指正交流...

扫描本地视频,我抽取出了三个分类和一个需要获取视频资源属性的枚举值

需要填的枚举值
typedef NS_ENUM(NSInteger , TFVideoAssetType){
    
    TFVideoAssetTypeVideoImageSource, //视频缩略图
    TFVideoAssetTypeVideoTimeLength,  //视频时长
    TFVideoAssetTypeVideoName,        //视频名称
};

最主要的三个方法

/**
 *  获取相册的授权
 *  @return YES/NO
 */
- (BOOL)authStatus;
/**
 *  视频的搜索结果
 *
 *  @return 返回的是一个数组集合,里面是PHAsset
 */
- (PHFetchResult *)assetsFetchResults;
/**
 *  获取所需要的Video属性
 *
 *  @param type TFVideoAssetType
 *
 *  @return 资源数组
 */
- (NSArray *)getVideoRelatedAttributesArray:(TFVideoAssetType)type;

下面来讲一下具体实现

//相册授权
- (BOOL)authStatus{
    PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus];
    if (author == PHAuthorizationStatusRestricted || author == PHAuthorizationStatusDenied) {
        return NO;
    }else{
        return YES;
    }
}
//获取搜索的结果
- (PHFetchResult *)assetsFetchResults{

    return [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:[self getFetchPhotosOptions]];
}
//筛选的规则和范围
- (PHFetchOptions *)getFetchPhotosOptions{
    
   PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc]init];
    
    //扫描的范围为:用户相册,iCloud分享,iTunes同步
    allPhotosOptions.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary | PHAssetSourceTypeCloudShared | PHAssetSourceTypeiTunesSynced;
    
    //排序的方式为:按时间排序
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    return allPhotosOptions;
}
//获取所需要的资源数组,缩略图,时长,视频名称
- (NSArray *)getVideoRelatedAttributesArray:(TFVideoAssetType)type{
    NSMutableArray *sourceArray = [NSMutableArray arrayWithCapacity:self.assetsFetchResults.count];
 
    for (PHAsset *asset in self.assetsFetchResults) {
       
        NSString *videoName = [asset valueForKey:@"filename"];
        
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) {
          //获取视频的缩略图
          UIImage *image = result;
          //视频时长
          NSString *timeLength = [self getVideoDurtion:[self getcalduration:asset]];
          
            switch (type) {
                case TFVideoAssetTypeVideoImageSource:
                    [sourceArray addObject:image];      //UIImage
                    break;
                case TFVideoAssetTypeVideoTimeLength:
                    [sourceArray addObject:timeLength]; //NSString
                    break;
                case TFVideoAssetTypeVideoName:
                    [sourceArray addObject:videoName];  //NSString
                    break;

                default:
                    break;
            }
            
         }];

    }
    
    return sourceArray;
}

如何使用以及点击如何播放的方法,我在demo里写的很清楚,大家可以参考下
demo下载地址:https://github.com/aiyakuaile/LocalVideo

你可能感兴趣的:(iOS9之后的扫描本地视频并播放)