博客> iOS调用摄像头 视频转码 计算视频的大小和视频的时间
iOS调用摄像头 视频转码 计算视频的大小和视频的时间
2017-10-19 18:14 评论:0 阅读:601 dongcaili2012
ios

相册选择图片或拍照上传图片

  • (UIImagePickerController *)imagePickerController { if (!_imagePickerController) { _imagePickerController = [[UIImagePickerController alloc] init]; _imagePickerController.delegate = self; _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; _imagePickerController.allowsEditing = YES; } return _imagePickerController; }

手机相册选择图片 _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [welf presentViewController:_imagePickerController animated:YES completion:nil]; 拍照 _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; _imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage]; _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; [welf presentViewController:_imagePickerController animated:YES completion:nil];

  • (void)imagePickerController:(UIImagePickerController )picker didFinishPickingImage:(UIImage )image editingInfo:(nullable NSDictionary *)editingInfo { NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo); [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *aImage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage]; // [self saveImage:aImage withName:@"currentImage.png"];

    [_avatarImageView setImage:aImage]; }

相册选择视频或录制视频

相册选择视频 UIImagePickerController controller = [[UIImagePickerController alloc] init]; [controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];// 设置类型 controller.allowsEditing = YES; NSMutableArray mediaTypes = [[NSMutableArray alloc] init]; [mediaTypes addObject:( NSString *)kUTTypeMovie]; [controller setMediaTypes:mediaTypes]; [controller setDelegate:self];

    [self presentViewController:controller animated:YES completion:^{
    }];

摄像头录制视频 UIImagePickerController pickerView = [[UIImagePickerController alloc] init]; pickerView.sourceType = UIImagePickerControllerSourceTypeCamera; NSArray availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; pickerView.mediaTypes = [NSArray arrayWithObject:availableMedia[1]]; pickerView.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo; pickerView.videoMaximumDuration = 60000;//允许录制的视频的最大时长 pickerView.delegate=self; [self presentViewController:pickerView animated:YES completion:^{ }]; //代理方法

  • (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info{

    _videoURL = [info objectForKey: UIImagePickerControllerMediaURL];

    NSLog(@"录制完后本身的文件大小%@",[NSString stringWithFormat:@"%ld kb", (long)[self getFileSize:[[_videoURL absoluteString] substringFromIndex:16]]]); NSLog(@"录制完后本身的时长%@",[NSString stringWithFormat:@"%.0f s", [self getVideoDuration:_videoURL]]); _hasVideo = YES; [self videoEncode];

    [self dismissViewControllerAnimated:YES completion:nil];

}

pragma mark 获取视频原有大小

  • (NSInteger) getFileSize:(NSString) path { NSFileManager filemanager = [[NSFileManager alloc]init]; if([filemanager fileExistsAtPath:path]){ NSDictionary attributes = [filemanager attributesOfItemAtPath:path error:nil]; NSNumber theFileSize; if ( (theFileSize = [attributes objectForKey:NSFileSize]) ) return [theFileSize intValue]/1024; else return -1; } else { return -1; } }

pragma mark 获取视频时长

  • (CGFloat) getVideoDuration:(NSURL) URL { NSDictionary opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:URL options:opts]; float second = 0; second = urlAsset.duration.value/urlAsset.duration.timescale; return second; }
收藏
0
sina weixin mail 回到顶部