调用系统UIImagePickerController录像并保存到指定的文件夹

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - 录像  
  2. - (void)recodVideo{  
  3.       
  4.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  5.         UIImagePickerController* pickerView = [[UIImagePickerController alloc] init];  
  6.         pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;  
  7.         NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];  
  8.         pickerView.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];  
  9.         pickerView.videoQuality = UIImagePickerControllerQualityTypeMedium;  
  10.         [self pushVieCtr:pickerView];  
  11.         //    pickerView.videoMaximumDuration = 60;  
  12.         pickerView.delegate = self;  
  13.         [pickerView release];  
  14.           
  15.     }  
  16.       
  17. }  
[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - UIImagePickerControllerDelegate  
  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  3. {  
  4.     _videoURL = [info[UIImagePickerControllerMediaURL] retain];  
  5.     NSLog(@"_videoURL = %@",_videoURL);  
  6.      
  7.     [picker.flipboardNavigationController popViewController];  
  8.    [self encodeVideoOrientation:_videoURL];  
  9.     NSLog(@"_videoURL.absoluteString %@",_videoURL.path);  
  10.       
  11.     //    NSString *videoPath = _videoURL.path;  
  12.     //    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)) {  
  13.     //        UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, NULL, NULL);  
  14.     //    }  
  15. }  

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. - (void)encodeVideoOrientation:(NSURL *)anOutputFileURL  
  2. {  
  3.       
  4.     _alert = [[UIAlertView alloc] init];  
  5.     [_alert setTitle:@"Waiting.."];  
  6.       
  7.     UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];  
  8.     activity.frame = CGRectMake(140,  
  9.                                 80,  
  10.                                 CGRectGetWidth(_alert.frame),  
  11.                                 CGRectGetHeight(_alert.frame));  
  12.     [_alert addSubview:activity];  
  13.     [activity startAnimating];  
  14.     [activity release];  
  15.     [_alert show];  
  16.     [_alert release];  
  17.     _startDate = [[NSDate date] retain];  
  18.       
  19.       
  20.     AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];  
  21.       
  22.     AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:videoAsset  
  23.                                                                           presetName:AVAssetExportPresetMediumQuality];  
  24.     NSDateFormatter* formater = [[NSDateFormatter alloc] init];  
  25.     [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];  
  26.     _mp4Path = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/videos/%@.mp4", [formater stringFromDate:[NSDate date]]] retain];  
  27.     [formater release];  
  28.       
  29.     assetExport.outputURL = [NSURL fileURLWithPath: _mp4Path];  
  30.     assetExport.shouldOptimizeForNetworkUse = YES;  
  31.     assetExport.outputFileType = AVFileTypeMPEG4;  
  32.     assetExport.videoComposition = [self getVideoComposition:videoAsset];  
  33.     [assetExport exportAsynchronouslyWithCompletionHandler:^{  
  34.         switch ([assetExport status]) {  
  35.             case AVAssetExportSessionStatusFailed:  
  36.             {  
  37.                 NSLog(@"AVAssetExportSessionStatusFailed!");  
  38.                 [_alert dismissWithClickedButtonIndex:0 animated:NO];  
  39.                 [self performSelectorOnMainThread:@selector(converFailed:) withObject:assetExport waitUntilDone:NO];  
  40.                 break;  
  41.             }  
  42.                   
  43.             case AVAssetExportSessionStatusCancelled:  
  44.                 NSLog(@"Export canceled");  
  45.                 [_alert dismissWithClickedButtonIndex:0  
  46.                                              animated:YES];  
  47.                 break;  
  48.             case AVAssetExportSessionStatusCompleted:  
  49.                 NSLog(@"Successful!");  
  50.                 [self performSelectorOnMainThread:@selector(convertFinish) withObject:nil waitUntilDone:NO];  
  51.                 break;  
  52.             default:  
  53.                 break;  
  54.         }  
  55.         [assetExport release];  
  56.     }];  
  57.       
  58. }  
  59.   
  60. #pragma mark - 解决录像保存角度问题  
  61.   
  62. -(AVMutableVideoComposition *) getVideoComposition:(AVAsset *)asset  
  63. {  
  64.     AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];  
  65.     AVMutableComposition *composition = [AVMutableComposition composition];  
  66.     AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];  
  67.     CGSize videoSize = videoTrack.naturalSize;  
  68.     BOOL isPortrait_ = [self isVideoPortrait:asset];  
  69.     if(isPortrait_) {  
  70.         NSLog(@"video is portrait ");  
  71.         videoSize = CGSizeMake(videoSize.height, videoSize.width);  
  72.     }  
  73.     composition.naturalSize     = videoSize;  
  74.     videoComposition.renderSize = videoSize;  
  75.     // videoComposition.renderSize = videoTrack.naturalSize; //  
  76.     videoComposition.frameDuration = CMTimeMakeWithSeconds( 1 / videoTrack.nominalFrameRate, 600);  
  77.       
  78.     AVMutableCompositionTrack *compositionVideoTrack;  
  79.     compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];  
  80.     [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];  
  81.     AVMutableVideoCompositionLayerInstruction *layerInst;  
  82.     layerInst = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];  
  83.     [layerInst setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];  
  84.     AVMutableVideoCompositionInstruction *inst = [AVMutableVideoCompositionInstruction videoCompositionInstruction];  
  85.     inst.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);  
  86.     inst.layerInstructions = [NSArray arrayWithObject:layerInst];  
  87.     videoComposition.instructions = [NSArray arrayWithObject:inst];  
  88.     return videoComposition;  
  89. }  

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. -(BOOL) isVideoPortrait:(AVAsset *)asset  
  2. {  
  3.     BOOL isPortrait = FALSE;  
  4.     NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];  
  5.     if([tracks    count] > 0) {  
  6.         AVAssetTrack *videoTrack = [tracks objectAtIndex:0];  
  7.           
  8.         CGAffineTransform t = videoTrack.preferredTransform;  
  9.         // Portrait  
  10.         if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0)  
  11.         {  
  12.             isPortrait = YES;  
  13.         }  
  14.         // PortraitUpsideDown  
  15.         if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)  {  
  16.               
  17.             isPortrait = YES;  
  18.         }  
  19.         // LandscapeRight  
  20.         if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)  
  21.         {  
  22.             isPortrait = FALSE;  
  23.         }  
  24.         // LandscapeLeft  
  25.         if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)  
  26.         {  
  27.             isPortrait = FALSE;  
  28.         }  
  29.     }  
  30.     return isPortrait;  
  31. }  
posted @ 2016-03-09 09:51  花满楼婷  阅读(409)  评论(0编辑  收藏  举报