如何跟踪由UIImagePickerController创建/选择的媒体?

人气:652 发布:2022-10-16 标签: ios uiimagepickercontroller

问题描述

我正在构建一个iOS应用程序,它允许用户通过录制或从Camera Roll中选择视频来从UIImagePickerController上传视频,以及播放所选的视频。我的问题是,我将如何继续引用以这种方式选择的视频?我希望这样做,以便如果视频仍然存在于设备上,我可以使用本地文件,而不是流式传输上载的文件。

时间

 imagePickerController:didFinishPickingMediaWithInfo:

返回以下位置的URL:

 [info objectForKey:UIImagePickerControllerMediaURL];

格式为:"file://localhost/private/var/mobile/Applications//TMP//trim.z2vLjx.MOV"

我认为"/tmp/"目录是临时的,因此不适合保存该位置的URL。

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

[result valueForProperty:ALAssetPropertyDate];

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

推荐答案

我终于找到了解决方案:

-(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);
        }];
    }
}
像往常一样,解决方案是通过更深入地阅读文档找到的。希望这能在某一时刻帮助其他人。

827