简体   繁体   English

从 ios 中的 UIImagePickerController 选择视频后如何避免压缩

[英]How to avoid compression after selecting video from UIImagePickerController in ios

I am using UIImagePickerController to select video from Gallery and it compress that video.I want to disable Compression but I don't find the way to do this.我正在使用 UIImagePickerController 处理来自 Gallery 的 select 视频并压缩该视频。我想禁用压缩,但我找不到执行此操作的方法。 I also tried with ELCImagePickerController it is showing video but it is looking like an image only there is no video icon or time duration like it shows in UIImagePickercontroller.How can I do it?我也尝试使用 ELCImagePickerController 它正在显示视频,但它看起来像一个图像,只是没有视频图标或持续时间,就像它在 UIImagePickercontroller 中显示的那样。我该怎么做?

Thanks.谢谢。

It doesn't look like it's possible to avoid compression using the UIImagePickerController. 看起来不可能避免使用UIImagePickerController进行压缩。 See this answer: 看到这个答案:

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

I've tried using imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh; 我试过使用imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh; but it still does compression. 但它仍然可以压缩。 Ugh. 啊。

EDIT: 编辑:

However, you can roll your own. 但是,您可以自己滚动。 This will allow access to the raw video files: 这将允许访问原始视频文件:

iOS 8 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
    }];
}

Look at the PhotoKit documentation for accessing collections (moments) and other options. 请参阅PhotoKit文档,以访问照片集(时刻)和其他选项。

Here is a sample app from Apple using PhotoKit that could be modified to be a photo picker: https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html 这是苹果公司使用PhotoKit的示例应用程序,可以将其修改为照片选择器: https : //developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

Here is a photo picker library on GitHub that uses PhotoKit that looks promising since it gives you the PHAsset objects for all the selected images/videos: https://github.com/guillermomuntaner/GMImagePicker 这是GitHub上使用PhotoKit的照片选择器库,它看起来很有希望,因为它为您提供了所有选定图像/视频的PHAsset对象: https : //github.com/guillermomuntaner/GMImagePicker

iOS 7 and below 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);
}];

With iOS 11 you can set the property videoExportPreset to AVAssetExportPresetPassthrough to get the original: 在iOS 11中,您可以将属性videoExportPreset设置为AVAssetExportPresetPassthrough以获取原始图片:

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

The "Video compression..." label just flashes for a few milliseconds and then the export is done. “视频压缩...”标签仅闪烁几毫秒,然后完成导出。

@Diego Renau almost had the correct answer. @迭戈·雷瑙(Diego Renau) 几乎是正确的答案。

Actually, you can get the original video URL, non-compressed version, via the following code: 实际上,您可以通过以下代码获得原始视频URL(非压缩版本):

- (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.
            }   
        }]; 
    }
}

As a reminder, the requestAVAssetForVideo call is asynchronous, so be careful when you want to store the url with a blocked variable outside the method calling block. 提醒一下,requestAVAssetForVideo调用是异步的,因此,当您要在方法调用块之外将URL与一个受阻止的变量存储在一起时,请务必小心。

With iOS 11 you can use the property "videoExportPreset". 在iOS 11中,您可以使用属性“ videoExportPreset”。 It's not the original but at least I can get more than 1280x720... 它不是原始的,但至少我可以获得超过1280x720的分辨率...

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

//AVAssetExportPreset640x480
//AVAssetExportPreset960x540
//AVAssetExportPreset1280x720
//AVAssetExportPreset1920x1080
//AVAssetExportPreset3840x2160

By using following code you can get the original video as AVAsset通过使用以下代码,您可以获得原始视频作为AVAsset
Swift version: Swift版本:

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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM