【问题标题】:Getting trimmed video file size from UIImagePickerController从 UIImagePickerController 获取修剪后的视频文件大小
【发布时间】:2012-04-12 05:47:58
【问题描述】:

使用UIImagePickerController 选择视频后,我可以从它的 [defaultRepresentation size].
但是,如果我启用选择器的修剪选项,[defaultRepresentation size] 会返回与原始未修剪视频相同的文件大小。

有没有人有办法检索修剪后的视频文件大小?

非常感谢...

是的,我应该包含一些代码。我坏了。

我想要实现的是让用户选择并修剪现有视频,然后上传修剪后的版本。我想要修剪版本的文件大小,以便在上传期间填充进度条。我也想要修剪后版本的持续时间。

调用方法是:

-(IBAction)selectVideoPressed:(id)sender
{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

{

    UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
    [videoPicker setDelegate: self];
    [videoPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [videoPicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil]];
    [videoPicker setVideoQuality:vvi.videoQuality];
    [videoPicker setAllowsEditing:YES];
    [videoPicker setModalTransitionStyle:gTransitionStyle];

    [self presentViewController:videoPicker animated:YES completion:Nil];

}

}

在用户修剪并按下选择后触发的委托方法是:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[vvi setRawSourceVideoFileURL:[info objectForKey:UIImagePickerControllerMediaURL]];
[vvi setSourceVideoURL:[info objectForKey:UIImagePickerControllerReferenceURL]];

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset)

{

    // Video metrics info.

    [vvi setVideoDuration:[myAsset valueForProperty:ALAssetPropertyDuration]];
    [vvi setVideoOrientation:[myAsset valueForProperty:ALAssetPropertyOrientation]];
    NSDate *videoDate = [myAsset valueForProperty:ALAssetPropertyDate];
    [vvi setVideoDateString:[videoDate descriptionWithLocale:[NSLocale currentLocale]]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:gShortDateFormat];
    [vvi setShortVideoDateString:[dateFormatter stringFromDate:videoDate]];

    ALAssetRepresentation *myAssetRepresentation = [myAsset defaultRepresentation];
    [vvi setVideoAssetSize:[NSNumber numberWithLongLong:[myAssetRepresentation size]]];

    NSLog(@"Duration:%@", vvi.videoDuration);
    NSLog(@"Size:%@", vvi.videoAssetSize);

};

 ....

vvi 只是我自己的自定义类。 两个 NSLog 返回原始未修剪视频的持续时间和大小。使用[vvi setRawSourceVideoFileURL:[info objectForKey:UIImagePickerControllerMediaURL]]; 指向的视频确实会返回正确修剪的视频。

非常感谢!

【问题讨论】:

  • 需要您尝试实现此目的的代码的 sn-p。
  • @hpiOSCoder 是的,我想代码会有所帮助...

标签: xcode set uiimagepickercontroller trim alasset


【解决方案1】:

这样的事情应该注意查找从 UIImagePickerController 返回的所选图像或视频的文件大小

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

        NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];

        //Error Container
        NSError *attributesError;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[videoUrl path] error:&attributesError];
        NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
        long long fileSize = [fileSizeNumber longLongValue];
}

【讨论】:

  • 这已经在其他地方被问过了,但这是最完整的答案!
  • 我们可以为这个请提供一个swift 3版本
猜你喜欢
  • 1970-01-01
  • 2011-09-02
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2012-07-17
  • 2011-01-14
相关资源
最近更新 更多