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

时间:2013-11-25 11:00:43

标签: iphone uiimagepickercontroller

我正在使用UIImagePickerController从Gallery中选择视频并压缩该视频。我想禁用压缩但我找不到这样做的方法。 我也尝试过使用ELCImagePickerController它显示视频,但它看起来像一个图像,只有没有视频图标或持续时间,就像它在UIImagePickercontroller中显示的那样。我可以这样做吗?

感谢。

4 个答案:

答案 0 :(得分:18)

看起来不可能使用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);
}];

答案 1 :(得分:12)

使用iOS 11,您可以将属性videoExportPreset设置为AVAssetExportPresetPassthrough以获取原始内容:

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

"视频压缩......"标签只闪烁几毫秒然后导出完成。

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

答案 2 :(得分:7)

实际上,您可以通过以下代码获取原始视频网址(非压缩版本):

- (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时要小心。

答案 3 :(得分:3)

使用iOS 11,您可以使用属性" videoExportPreset"。 它不是原版,但至少我可以获得超过1280x720 ......

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

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