【问题标题】:IOS -how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path?IOS - UISaveVideoAtPathToSavedPhotosAlbum 如何返回保存的视频路径?
【发布时间】:2023-04-01 11:48:02
【问题描述】:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];


    //get the videoURL 
    NSString *tempFilePath = [videoURL path];


    if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath))
    {
      // Copy it to the camera roll.
      UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:), tempFilePath);
    } 
}

我使用 UISaveVideoAtPathToSavedPhotosAlbum 来保存录制的视频。我想知道我保存录制视频的相册中的绝对路径。

我怎样才能得到保存的路径? UISaveVideoAtPathToSavedPhotosAlbum 不返回任何内容。

并且在回调函数video:didFinishSavingWithError:contextInfo: 中仍然没有路径信息。

【问题讨论】:

    标签: iphone ios video


    【解决方案1】:

    据我所知.. 您无法获取相册中保存的视频的路径。如果您希望从您的应用程序重播文件列表。您可以在应用程序中包含所有视频。

    以下是您放置在 didFinishPickingMedia 中以将视频存储在辅助文档中的示例代码。这样您就可以跟踪它。

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", 1]];
        NSString *fileName = [NSString stringWithFormat:@"%@ :%@.%@", itsIncidentType, [dateFormatter stringFromDate:incidentDate], @"mp4"];
        [dateFormatter release];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
        NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
        NSData *webData = [NSData dataWithContentsOfURL:videoURL];
        self.itsVideoName = fileName;
        [webData writeToFile:[NSString stringWithFormat:@"%@/%@",dataPath,fileName] atomically:TRUE];
    

    希望对你有所帮助..

    【讨论】:

      【解决方案2】:

      我最近遇到了类似的问题,就这样解决了。

      使用两种方法创建 PHAsset 类别

      PHAsset+Picking.h

      #import <Photos/Photos.h>
      
      @interface PHAsset (Picking)
      
      + (PHAsset *)retrievePHAssetWithLocalIdentifier:(NSString *)identifier;
      
      + (void)saveVideoFromCameraToPhotoAlbumWithInfo:(NSDictionary *)info
                                           completion:(void(^)(PHAsset * _Nullable asset))completion;
      
      @end
      

      PHAsset+Picking.m

      @implementation PHAsset (Picking)
      
      + (PHAsset *)retrievePHAssetWithLocalIdentifier:(NSString *)identifier {
          PHAsset *asset = nil;
          if (identifier) {
              PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[identifier] options:nil];
              asset = result.firstObject;
          }
      
          return asset;
      }
      
      + (void)saveVideoFromCameraToPhotoAlbumWithInfo:(NSDictionary *)info 
                                           completion:(void(^)(PHAsset * _Nullable asset))completion
      {
          // get URL to file from picking media info
          NSURL *url = info[UIImagePickerControllerMediaURL];
          __block PHAssetChangeRequest *changeRequest = nil;
          __block PHObjectPlaceholder *assetPlaceholder = nil;
          // save video file to library
          [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
              changeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
              assetPlaceholder = changeRequest.placeholderForCreatedAsset;
          } completionHandler:^(BOOL success, NSError *error) {
              if (success) {
                  // get saved object as PHAsset 
                  PHAsset *asset = [PHAsset retrievePHAssetWithLocalIdentifier:assetPlaceholder.localIdentifier];
                  completion(asset);
              } else {
                  completion(nil);
              }
          }];
      }
      
      @end
      

      然后在里面使用saveVideoFromCameraToPhotoAlbumWithInfo方法

      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
          NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
          if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
              Weakify(self);
              void (^processAsset)(PHAsset *, NSDictionary *) = ^void(PHAsset *asset, NSDictionary *mediaInfo) {
                  [PHImageManager.defaultManager requestAVAssetForVideo:asset
                                                            options:nil
                                                      resultHandler:^(AVAsset *asset,
                                                                      AVAudioMix *audioMix,
                                                                      NSDictionary *assetInfo)
                   {
                       Strongify(self);
                       if ([asset respondsToSelector:@selector(URL)]) {
      
                           // URL to video file here -->
                           NSURL *videoURL = [asset performSelector:@selector(URL)];
                           // <--
      
                       } else {
                           // asset hasn't property URL
                           // it is can be AVComposition (e.g. user chose slo-mo video)
                       }
                   }];
              };
      
              if (UIImagePickerControllerSourceTypeCamera == picker.sourceType) {
                  // save video from camera
                  [PHAsset saveVideoFromCameraToPhotoAlbumWithInfo:info completion:^(PHAsset *asset) {
                      // processing saved asset
                      processAsset(asset, info);
                  }];
              } else {
                  // processing existing asset from library
                  processAsset(info[UIImagePickerControllerPHAsset], info);
              }
          } else {
              // image processing
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-04
        相关资源
        最近更新 更多