音视频资源保存与本地获取

一、将视频保存本地设备

Git地址:https://github.com/gaoguangxiao/SaveToCamera

- (void)saveAction{

    NSMutableArray *video = [NSMutableArray arrayWithCapacity:3];

    NSArray *moves = [[NSBundle mainBundle]pathsForResourcesOfType:@"mov" inDirectory:nil];

    [video addObjectsFromArray:moves];

    for (id item in video) {

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(item)) {

            NSLog(@"保存成功");

            UISaveVideoAtPathToSavedPhotosAlbum(item, nil, nil, NULL)

       }

   }

}

二、本地音视频资源的获取

//获取所有资源的集合,按照时间进行排序

PHFetchOptions *options = [[PHFetchOptions alloc] init];

options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];

//这时 assetsFetchResults 中包含的,应该就是各个资源(PHAsset)

for (NSInteger i = 0; i < assetsFetchResults.count; i++) {

        // 获取一个资源(PHAsset)

        PHAsset *phAsset = assetsFetchResults[i];

        if (phAsset.mediaType == PHAssetMediaTypeVideo) {

            PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];

            options.version = PHImageRequestOptionsVersionCurrent;

            options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;

            PHImageManager *manager = [PHImageManager defaultManager];

            [manager requestAVAssetForVideo:phAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {

                AVURLAsset *urlAsset = (AVURLAsset *)asset

                NSURL *url = urlAsset.URL;

                NSData *data = [NSData dataWithContentsOfURL:url];

                NSLog(@"%@",data);

            }];

        }else if (phAsset.mediaType == PHAssetMediaTypeImage){

            PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];

            options.version = PHImageRequestOptionsVersionCurrent;

              //返回图片的质量类型 (效率高,质量低)

            options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;

            //同步请求获取iCloud图片(默认为NO)

            //options.synchronous = YES;

             [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {

            data = [NSData dataWithData:imageData];

            if (completion && data.length > 0) {

                completion(mediaName, data);

            }

        }];

        }

    }

三、图片选择资源库

本地资源保存:https://github.com/gaoguangxiao/SaveToCamera

github地址:https://github.com/honeycao/ACMediaFrame

你可能感兴趣的:(音视频资源保存与本地获取)