【问题标题】:Get video size after Save this video to Photo Library in iOS将此视频保存到 iOS 中的照片库后获取视频大小
【发布时间】:2014-02-02 01:50:49
【问题描述】:

拍摄视频后,我使用UIImagePickerController 创建了相机。我点击了使用视频,我将此视频保存到照片库。现在我想知道这个视频的大小。我使用AlAssetLibrary 将视频保存到照片库。如何获取刚刚拍摄的视频的大小?请帮我。提前致谢。

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

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){}
         ];
    }

}

我尝试了以下代码来获取视频的大小但不起作用:

 ALAssetRepresentation *rep = [alasset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSError *error = nil;
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

【问题讨论】:

  • 我不知道,但是当我的应用程序在使用上面的代码获取视频大小时崩溃了。
  • 可能视频很大,你的应用被内存压力杀死了。
  • @RhythmicFistman:是的,我已经完成了。但是我在取消选择时卡在删除覆盖图像中。你能告诉我在 UICollectionView 中取消选择时如何删除覆盖吗?谢谢
  • 这是一个话题变化,所以问一个新问题。

标签: ios iphone objective-c uiimagepickercontroller alassetslibrary


【解决方案1】:

1) 请看this 问题以获取您的视频。

2) 将视频转换为 NSData 后,请参阅 this question

3) 现在使用 NSData 的 length function

它会得到你的视频文件的长度。

【讨论】:

  • 是的,我已经阅读了上面的链接,但是在尝试获取视频大小时我的应用程序崩溃了。你能帮助我吗?谢谢
【解决方案2】:

你可以通过三种方式做到这一点

1.使用NSData length

    ALAssetRepresentation *rep = [alasset defaultRepresentation];
    Byte *buffer = (Byte*)malloc(rep.size);
    NSError *error = nil;
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:&error];
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    NSLog(@"Size of video %d",data.length); 

2.利用ALAssetRepresentationsize

3.最坏情况:使用指向已保存视频文件的videoPathURL,使用ALAsset再次检索并计算大小。

【讨论】:

    【解决方案3】:

    您也可以使用它来获取视频大小:

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:YOURURL];
    
    CMTime duration = playerItem.duration;
    float seconds = CMTimeGetSeconds(duration);
    

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多