美文网首页
iOS9,ALAssetsLibrary存储视频代码过期,使用新

iOS9,ALAssetsLibrary存储视频代码过期,使用新

作者: 笑笑菜鸟 | 来源:发表于2021-04-17 18:08 被阅读0次

这里不对视频的录制进行过多的阐述,发现ALAssetsLibrary存储视频代码在iOS9过期的弟兄们应该都已经做到了用ALAssetsLibrary存储视频这一块了,下面直接将贴iOS9之前使用的ALAssetsLibrary存储视频代码块替换成使用新的PHPhotoLibrary存储视频的代码块。

首先我们看ALAssetsLibrary存储视频的代码:

ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
__weaktypeof(self) weakSelf =self;
[lib writeVideoAtPathToSavedPhotosAlbum:self.videoURL completionBlock:^(NSURL *assetURL, NSError *error) {
    if (weakSelf.shootSuccessBlock) {
        weakSelf.shootSuccessBlock(weakSelf.videoURL, image);
    }
}];

上面可以看出,原来实现视频存储到本地的代码非常之少。

PHPhotoLibrary存储视频前提一定要获取相册权限,否则写入视频会崩溃:

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
NSString *message = [NSString stringWithFormat:@"请在iPhone的\"设置-隐私-相机\"选项中,允许%@访问你的相机", appName];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [weakSelf dismissViewControllerAnimated:YES completion:nil];
}];

UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"系统设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSURL *url = [NSURL  URLWithString:UIApplicationOpenSettingsURLString];
     if ([[UIApplication sharedApplication] canOpenURL:url]) {
         [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
     }
}];

[alertController addAction:okAction];
[alertController addAction:setAction];

[self presentViewController:alertController animated:YES completion:nil]; 

接下来贴PHPhotoLibrary存储视频的代码块:

__weak typeof(self) weakSelf = self;
PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
dispatch_async(dispatch_get_main_queue(), ^{
    NSError *error = nil;
    __block NSString *assetId = nil;
    __block NSString *assetCollectionId = nil;

// 保存视频到【Camera Roll】(相机胶卷)
    [library performChangesAndWait:^{
        assetId = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:weakSelf.videoURL].placeholderForCreatedAsset.localIdentifier;
} error:&error];

     NSLog(@"error1: %@", error);

// 获取曾经创建过的自定义视频相册名字
    PHAssetCollection *createdAssetCollection = nil;
    PHFetchResult <PHAssetCollection*> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
     for (PHAssetCollection *assetCollection in assetCollections) {
          if ([assetCollection.localizedTitle isEqualToString:AssetCollectionName]) {
              createdAssetCollection = assetCollection;
              break;
          }
     }

//如果这个自定义框架没有创建过
    if (createdAssetCollection == nil) {
//创建新的[自定义的 Album](相簿\相册)
        [library performChangesAndWait:^{
            assetCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:AssetCollectionName].placeholderForCreatedAssetCollection.localIdentifier;
        } error:&error];
        NSLog(@"error2: %@", error);
//抓取刚创建完的视频相册对象
       createdAssetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionId] options:nil].firstObject;
    }

// 将【Camera Roll】(相机胶卷)的视频 添加到【自定义Album】(相簿\相册)中
    [library performChangesAndWait:^{
        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];

        [request addAssets:[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil]];

    } error:&error];
    NSLog(@"error3: %@", error);

// 提示信息
    if (self.shootCompletionBlock) {
        if (error) {
             NSLog(@"保存视频失败!");
             self.shootCompletionBlock(nil, nil, error);
        } else {
             NSLog(@"保存视频成功!");
            self.shootCompletionBlock(weakSelf.videoURL, image, nil);
        }
     }
});

相关文章

网友评论

      本文标题:iOS9,ALAssetsLibrary存储视频代码过期,使用新

      本文链接:https://www.haomeiwen.com/subject/sscplltx.html