187实现录制视频功能

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

PS:对于 Video 选项,会调用摄像头和麦克风,需要真机才能测试。

UIImagePickerControllerQualityType(视频质量类型枚举):

经过真机测试,录制30秒的视频,清晰度和大小由高到低为:

UIImagePickerControllerQualityTypeIFrame1280x720 -- 135.6 MB

UIImagePickerControllerQualityTypeIFrame960x540 -- 100.1 MB

UIImagePickerControllerQualityTypeHigh -- 61.8 MB

UIImagePickerControllerQualityType640x480 -- 12.7 MB(清晰度和大小来说,更推荐这个)

UIImagePickerControllerQualityTypeMedium -- 2.8 MB(默认值)

UIImagePickerControllerQualityTypeLow -- 721.4 KB(无法直视的模糊啊)

PCH File 的作用:

PCH File 在 Xcode 6中默认不会添加,这里如果我们工程文件多处地方需要调用到框架头文件时,我们可以考虑通过自行添加一个 PCH File,然后在 Build Settings 中的PRefix Header 项设置引用此文件;这样我们就不需要在需要调用它的地方通过 import 来导入框架头文件了。

PS:Prefix Header 项设置引用此文件如果不直接用「FirstBook187/PrefixHeader.pch」的话,也可以用「$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch」,在这里:

$(SRCROOT) 表示工程的相对路径:/Users/Kenmu/Documents/iOSDevelopment/FirstBook187

$(PROJECT_NAME)表示工程名称:FirstBook187

实际上「$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch」就被自动识别为:「/Users/Kenmu/Documents/iOSDevelopment/FirstBook187/FirstBook187/PrefixHeader.pch」了

关键操作:

效果如下:

ViewController.h

1#import<UIKit/UIKit.h>23@interfaceViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>45@end

ViewController.m

1#import"ViewController.h"2#import"sys/utsname.h"34@interfaceViewController ()5- (void)layoutUI;6- (void)showActionSheet:(UIBarButtonItem *)sender;7- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo;8@end910@implementationViewController1112- (void)viewDidLoad {13[super viewDidLoad];1415[self layoutUI];16}1718- (void)didReceiveMemoryWarning {19[super didReceiveMemoryWarning];20//Dispose of any resources that can be recreated.21}2223- (void)viewWillAppear:(BOOL)animated {24[super viewWillAppear:animated];25[self.navigationController setNavigationBarHidden:NO animated:animated];26[self.navigationController setToolbarHidden:NO animated:animated];27}2829- (void)layoutUI {30self.navigationItem.title =@"实现录制视频功能";31self.view.backgroundColor =[UIColor whiteColor];32UIBarButtonItem *barBtnVideo =[[UIBarButtonItem alloc]33initWithBarButtonSystemItem:UIBarButtonSystemItemCamera34target:self35action:@selector(showActionSheet:)];36self.toolbarItems =@[barBtnVideo];37}3839- (void)showActionSheet:(UIBarButtonItem *)sender {40UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"录制视频"41delegate:self42cancelButtonTitle:@"取消"43destructiveButtonTitle:nil44otherButtonTitles:@"PhotoLibrary",@"Video", nil];45[actionSheet showFromToolbar:self.navigationController.toolbar];46}4748- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo {49if(error) {50NSLog(@"%@", [error localizedDescription]);51}52}5354#pragmamark - UIActionSheetDelegate55- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {56if(buttonIndex !=actionSheet.cancelButtonIndex) {57UIImagePickerControllerSourceType sourceType =buttonIndex;58if([UIImagePickerController isSourceTypeAvailable:sourceType]) {59UIImagePickerController *imagePickerController =[[UIImagePickerController alloc] init];60imagePickerController.delegate=self;61imagePickerController.sourceType =sourceType;62imagePickerController.videoQuality = UIImagePickerControllerQualityType640x480;//设置视频质量;默认值为UIImagePickerControllerQualityTypeMedium63imagePickerController.videoMaximumDuration =30;//录制视频的时间,单位为秒;默认值为10分钟=600秒,这里设置为30秒6465NSArray *arrMediaType =[UIImagePickerController availableMediaTypesForSourceType:sourceType];66if([arrMediaType containsObject:(NSString *)kUTTypeMovie]) {//为了使用kUTTypeMovie,这里需要导入<MobileCoreServices/MobileCoreServices.h>67imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie];68}else{69NSLog(@"%@ is not available.", kUTTypeMovie);70}7172[self presentViewController:imagePickerController73animated:YES74completion:nil];75}76}7778/*79UIImagePickerControllerQualityType(视频质量类型枚举):经过真机测试,录制30秒的视频,清晰度和大小由高到低为:80UIImagePickerControllerQualityTypeIFrame1280x720 -- 135.6 MB81UIImagePickerControllerQualityTypeIFrame960x540 -- 100.1 MB82UIImagePickerControllerQualityTypeHigh -- 61.8 MB83UIImagePickerControllerQualityType640x480 -- 12.7 MB(清晰度和大小来说,更推荐这个)84UIImagePickerControllerQualityTypeMedium -- 2.8 MB(默认值)85UIImagePickerControllerQualityTypeLow -- 721.4 KB(无法直视的模糊啊)868788typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {89UIImagePickerControllerQualityTypeHigh = 0, // highest quality90UIImagePickerControllerQualityTypeMedium = 1, // medium quality, suitable for transmission via Wi-Fi91UIImagePickerControllerQualityTypeLow = 2, // lowest quality, suitable for tranmission via cellular network92#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_093UIImagePickerControllerQualityType640x480 = 3, // VGA quality94#endif95#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_096UIImagePickerControllerQualityTypeIFrame1280x720 = 4,97UIImagePickerControllerQualityTypeIFrame960x540 = 598#endif99};100*/101}102103#pragmamark - UIImagePickerControllerDelegate104- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {105//判断是否是视频106NSString *mediaType =[info objectForKey:UIImagePickerControllerMediaType];107if([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {108NSURL *mediaURL =[info objectForKey:UIImagePickerControllerMediaURL];109NSString *mediaPath =[mediaURL path];110//判断视频路径是否支持被保存到图片库中111if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(mediaPath)) {112//将视频保存到相册113UISaveVideoAtPathToSavedPhotosAlbum(mediaPath,114self,115@selector(video:didFinishSavingWithError:contextInfo:),116NULL);117}else{118NSLog(@"视频路径不支持被保存到图片库中");119}120}121122123//把图片保存到相册124//UIImageWriteToSavedPhotosAlbum(imgChoice,125//self,126//@selector(image:didFinishSavingWithError:contextInfo:),127//NULL);128129[self imagePickerControllerDidCancel:picker];130}131132- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {133[self dismissViewControllerAnimated:YES completion:nil];134}135136@end

AppDelegate.h

1#import<UIKit/UIKit.h>23@interfaceAppDelegate : UIResponder <UIapplicationDelegate>4@property (strong, nonatomic) UIWindow *window;5@property (strong, nonatomic) UINavigationController *navigationController;67@end

AppDelegate.m

1#import"AppDelegate.h"2#import"ViewController.h"34@interfaceAppDelegate ()5@end67@implementationAppDelegate89- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {10_window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];11ViewController *viewController =[[ViewController alloc] init];12_navigationController =[[UINavigationController alloc] initWithRootViewController:viewController];13_window.rootViewController =_navigationController;14//[_window addSubview:_navigationController.view];//当_window.rootViewController关联时,这一句可有可无15[_window makeKeyAndVisible];16returnYES;17}1819- (void)applicationWillResignActive:(UIApplication *)application {20}2122- (void)applicationDidEnterBackground:(UIApplication *)application {23}2425- (void)applicationWillEnterForeground:(UIApplication *)application {26}2728- (void)applicationDidBecomeActive:(UIApplication *)application {29}3031- (void)applicationWillTerminate:(UIApplication *)application {32}3334@end

PrefixHeader.pch

1#ifndef FirstBook187_PrefixHeader_pch2#defineFirstBook187_PrefixHeader_pch34//Include any system framework and library headers here that should be included in all compilation units.5//You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.6#import<MobileCoreServices/MobileCoreServices.h>78#endif

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航