[英]Avoid Video Compression when Selecting Movie with UIImagePickerController?
[英]How to avoid compression after selecting video from UIImagePickerController in ios
我正在使用 UIImagePickerController 處理來自 Gallery 的 select 視頻並壓縮該視頻。我想禁用壓縮,但我找不到執行此操作的方法。 我也嘗試使用 ELCImagePickerController 它正在顯示視頻,但它看起來像一個圖像,只是沒有視頻圖標或持續時間,就像它在 UIImagePickercontroller 中顯示的那樣。我該怎么做?
謝謝。
看起來不可能避免使用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文檔,以訪問照片集(時刻)和其他選項。
這是蘋果公司使用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);
}];
在iOS 11中,您可以將屬性videoExportPreset
設置為AVAssetExportPresetPassthrough
以獲取原始圖片:
if #available(iOS 11.0, *) {
picker.videoExportPreset = AVAssetExportPresetPassthrough
}
“視頻壓縮...”標簽僅閃爍幾毫秒,然后完成導出。
@迭戈·雷瑙(Diego Renau) 幾乎是正確的答案。
實際上,您可以通過以下代碼獲得原始視頻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.
}
}];
}
}
提醒一下,requestAVAssetForVideo調用是異步的,因此,當您要在方法調用塊之外將URL與一個受阻止的變量存儲在一起時,請務必小心。
在iOS 11中,您可以使用屬性“ videoExportPreset”。 它不是原始的,但至少我可以獲得超過1280x720的分辨率...
if #available(iOS 11.0, *) {
picker.videoExportPreset = AVAssetExportPreset1920x1080
} else {
// Fallback on earlier versions
}
//AVAssetExportPreset640x480
//AVAssetExportPreset960x540
//AVAssetExportPreset1280x720
//AVAssetExportPreset1920x1080
//AVAssetExportPreset3840x2160
通過使用以下代碼,您可以獲得原始視頻作為AVAsset
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.