2012-04-12 77 views
1

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

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

非常感谢...

是的,我应该包括一些代码。我不好。

我试图做到的,是让用户选择和修整现有的视频,然后上传修剪的版本。我想要修剪版本的文件大小,以便在上传过程中填充进度栏。我也想修剪版本的持续时间。

的调用方法:

-(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]];的视频确实会返回经过适当修剪的视频。

非常感谢!

+0

代码片段ü试图实现这一目标,需要返回。 – 2012-04-12 05:52:48

+0

@ hpiOSCoder是的,我猜代码会帮助... – 2012-04-13 16:22:15

回答

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]; 
} 
+0

这已被问到其他地方,但这是最完整的答案! – elliotrock 2015-03-28 11:50:29

+0

我们可以为这个PLZ有一个快速的3版本 – 2017-02-15 11:09:59