【问题标题】:How to avoid compression after selecting video from UIImagePickerController in ios从ios中的UIImagePickerController选择视频后如何避免压缩
【发布时间】:2013-11-25 11:00:43
【问题描述】:

我正在使用 UIImagePickerController 从图库中选择视频并压缩该视频。我想禁用压缩但我找不到执行此操作的方法。 我还尝试使用 ELCImagePickerController,它正在显示视频,但它看起来像一个图像,只是没有视频图标或持续时间,就像它在 UIImagePickercontroller 中显示的那样。我该怎么做?

谢谢。

【问题讨论】:

    标签: iphone uiimagepickercontroller


    【解决方案1】:

    在 iOS 11 中,您可以将属性 videoExportPreset 设置为 AVAssetExportPresetPassthrough 以获取原件:

    if #available(iOS 11.0, *) {
        picker.videoExportPreset = AVAssetExportPresetPassthrough
    }
    

    “视频压缩...”标签仅闪烁几毫秒,然后导出完成。

    @Diego Renau 几乎得到了正确答案。

    【讨论】:

    • 我不想要这个视频压缩标签!!!你有没有办法删除这个
    • 对不起,我不知道。
    • 我们无法从图像选择器中删除压缩。使用 PHAsset。
    • 在 iOS 15 上,这使得压缩速度更快,但在我的 iPhone 上仍然需要很长时间
    【解决方案2】:

    使用 UIImagePickerController 似乎无法避免压缩。看到这个答案:

    https://stackoverflow.com/a/5893066/406152

    我尝试过使用imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;,但它仍然会进行压缩。呃。

    编辑:

    但是,您可以自己滚动。这将允许访问原始视频文件:

    iOS 8

    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
    for (PHAsset *asset in assetsFetchResult) {
        PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
        videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;
    
        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:videoRequestOptions resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
            // the AVAsset object represents the original video file
        }];
    }
    

    查看 PhotoKit 文档以访问集合(时刻)和其他选项。

    这是一个来自 Apple 的示例应用,它使用 PhotoKit,可以修改为照片选择器:https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

    这是 GitHub 上的一个照片选择器库,它使用 PhotoKit 看起来很有前途,因为它为您提供了所有选定图像/视频的 PHAsset 对象:https://github.com/guillermomuntaner/GMImagePicker

    iOS 7 及更低版本

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            // If you want, you can filter just pictures or videos
            // I just need videos so I do this:
            [group setAssetsFilter:[ALAssetsFilter allVideos]];
    
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
                if (asset){
                    // You can now add this ALAsset in your own video picker.
                    // Note that you can only access the ALAsset as long as 
                    // you maintain a reference to the ALAssetsLibrary
    
                    // Or if you want to process the video, you can create an AVAsset:
                    NSURL *url = asset.defaultRepresentation.url;
                    AVAsset *videoAsset = [AVAsset assetWithURL:url];
                }
            }];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"error enumerating AssetLibrary groups %@\n", error);
    }];
    

    【讨论】:

      【解决方案3】:

      其实可以通过以下代码获取原始视频网址,非压缩版:

      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
          NSString *mediaType = info[UIImagePickerControllerMediaType];
          NSString *videoString = (NSString *)kUTTypeVideo;
          NSString *movieString = (NSString *)kUTTypeMovie;
      
          if ([mediaType isEqualToString:videoString] || [mediaType isEqualToString:movieString]) {
              NSURL *videoRef = info[UIImagePickerControllerReferenceURL];
              PHFetchResult *refResult = [PHAsset fetchAssetsWithALAssetURLs:@[videoRef] options:nil];                                                                                                                                        
              PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
              videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;
              [[PHImageManager defaultManager] requestAVAssetForVideo:[refResult firstObject] options:videoRequestOptions resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
                  if ([asset isKindOfClass:[AVURLAsset class]]) {
                        NSURL *originURL = [(AVURLAsset *)asset URL];
                        // Now you have the URL of the original video.
                  }   
              }]; 
          }
      }
      

      提醒一下,requestAVAssetForVideo 调用是异步的,所以当你想在方法调用块之外存储带有阻塞变量的 url 时要小心。

      【讨论】:

        【解决方案4】:

        在 iOS 11 中,您可以使用属性“videoExportPreset”。 不是原版但至少我能得到超过 1280x720...

        if #available(iOS 11.0, *) {
                 picker.videoExportPreset = AVAssetExportPreset1920x1080
        } else {
                    // Fallback on earlier versions 
        }
        
        //AVAssetExportPreset640x480
        //AVAssetExportPreset960x540
        //AVAssetExportPreset1280x720
        //AVAssetExportPreset1920x1080
        //AVAssetExportPreset3840x2160
        

        【讨论】:

          【解决方案5】:

          通过使用以下代码,您可以获得原始视频AVAsset
          斯威夫特版本:

          func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                  
                  let phAsset = info[.phAsset] as? PHAsset
                  let phVideoOptions = PHVideoRequestOptions()
                  phVideoOptions.version = .original
                  PHImageManager().requestAVAsset(forVideo: phAsset!, options: phVideoOptions) { [self] asset, audioMix, info in
                      DispatchQueue.main.async {
                          if ((asset?.isKind(of: AVURLAsset.self)) != nil) {
                             // Now, you can use asset
                          }
                      }
                      
                  }
             
                imagePickerController.dismiss(animated: true, completion: nil)
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多