我正在尝试从照片库中选择/压缩视频,但是当我获取持续时间和创建日期时,它们都返回null(持续时间默认为0.0秒)。我不确定在这里是否做错了什么。

- (void)imagePickerController:(UIImagePickerController *)uploadPick didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

 if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
 {
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    //Video Duration:
            MPMoviePlayerController *mp = [[MPMoviePlayerController alloc]
                                   initWithContentURL:videoURL];

    VideoTime.text = [NSString stringWithFormat:@"Time: %.2f", mp.duration];

    //Video Creation Date
    NSDictionary *metadataDictionary = (NSDictionary *)[info valueForKey:UIImagePickerControllerMediaMetadata];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *stringDate = [dateFormatter stringFromDate:metadataDictionary.fileCreationDate];
    [dateFormatter release];
    VideoDateTaken.text = [NSString stringWithFormat:@"Date Taken: %@", stringDate];

 }
}

最佳答案

根据documentationUIImagePickerControllerMediaMetadata仅对静止图像有效:


  仅当使用来源类型为
  设置为UIImagePickerControllerSourceTypeCamera,并且仅适用于
  静止图像。


为了获得所需的元数据,请使用ALAssetmetadata方法。

关于ios - 从ios库中的视频获取持续时间和创建日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14451333/

10-15 06:38