【问题标题】:How can I keep track of media created/chosen by UIImagePickerController?如何跟踪 UIImagePickerController 创建/选择的媒体?
【发布时间】:2011-12-12 12:07:08
【问题描述】:

我正在构建一个 iOS 应用程序,该应用程序允许用户从 UIImagePickerController 上传视频,方法是录制或从相机胶卷中选择视频,以及播放所选视频。我的问题是,我将如何保留对以这种方式选择的视频的引用?我想这样做,以便如果设备上仍然存在视频,我可以使用本地文件而不是流式传输上传的文件。

什么时候

 imagePickerController:didFinishPickingMediaWithInfo:

返回,网址在:

 [info objectForKey:UIImagePickerControllerMediaURL];

格式为:“file://localhost/private/var/mobile/Applications//tmp//trim.z2vLjx.MOV”

我相信“/tmp/”目录是临时的,因此不适合保存该位置的 URL。

我可以通过 ALAssetsLibrary 获取设备上的所有视频,但是因为我没有办法区分它们,所以这对我没有帮助。我一直在尝试使用:

[result valueForProperty:ALAssetPropertyDate];

为了区分视频,但我需要一种从 UIImagePickerController 获取创建日期的方法才能有用。

【问题讨论】:

    标签: ios uiimagepickercontroller


    【解决方案1】:

    我终于找到了解决办法:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    if(CFStringCompare((CFStringRef) mediaType,  kUTTypeMovie, 0) == kCFCompareEqualTo)
    {
        //Dismiss the media picker view
        [picker dismissModalViewControllerAnimated:YES];
    
        //Get the URL of the chosen content, then get the data from that URL
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *webData = [NSData dataWithContentsOfURL:videoURL];
    
        //Gets the path for the URL, to allow it to be saved to the camera roll
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
        {
            ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
    
            //The key UIImagePickerControllerReferenceURL allows you to get an ALAsset, which then allows you to get metadata (such as the date the media was created)
            [lib assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
                NSLog(@"created: %@", [asset valueForProperty:ALAssetPropertyDate]);
            } failureBlock:^(NSError *error) {
                NSLog(@"error: %@", error);
            }];
        }
    }
    

    像往常一样,通过更彻底地阅读文档找到了解决方案。希望这会在某个时候对其他人有所帮助。

    【讨论】:

      【解决方案2】:

      您可以轻松记录设备上的视频。要么保留一个数据库(我认为这太多了),要么只是一个包含视频列表的文件。在该列表中,您可以拥有资产的 URL。

      【讨论】:

      • 或者内容的 MD5sum,所以你没有重复
      • 嘿,谢谢,我已经想到了,不要认为我的问题表达得很好,我会更新它。
      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 2022-12-31
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      相关资源
      最近更新 更多