xcode - 从 UIImagePickerController 获取修剪后的视频文件大小

标签 xcode set uiimagepickercontroller trim alasset

使用 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];

}

}

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

-(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]];确实返回正确修剪的视频。

非常感谢!

最佳答案

类似这样的事情应该注意查找从 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];
}

关于xcode - 从 UIImagePickerController 获取修剪后的视频文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10118056/

相关文章:

objective-c - Xcode 中的基本缩进设置

ios - Xcode 4.5 : dependencies not updated when using workspaces

MySQL 函数 - 错误代码 : 1415 Not allowed to return a result set from a function

ios - 打开 Thread Sanitizer 会产生信号 SIGABRT

c++ - 如何更新一组 std::pair 中的成员?

python - 如何用特定集合中的元素填充 python 中的列表?

ios - View 未显示在 didFinishPickingMediaWithInfo 中

ios - 如何在 Swift 3.1 中使用 UIImagePickerControllerDelegate 获取目录(本地路径)?

ios - 如何快速打开电影画廊

ios - 模拟器运行时不可用