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>2 3 @interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>4 5 @end
ViewController.m
1 #import "ViewController.h" 2 #import "sys/utsname.h" 3 4 @interface ViewController () 5 - (void)layoutUI; 6 - (void)showActionSheet:(UIBarButtonItem *)sender; 7 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 [self layoutUI]; 16 } 17 18 - (void)didReceiveMemoryWarning { 19 [super didReceiveMemoryWarning]; 20 // Dispose of any resources that can be recreated. 21 } 22 23 - (void)viewWillAppear:(BOOL)animated { 24 [super viewWillAppear:animated]; 25 [self.navigationController setNavigationBarHidden:NO animated:animated]; 26 [self.navigationController setToolbarHidden:NO animated:animated]; 27 } 28 29 - (void)layoutUI { 30 self.navigationItem.title = @"实现录制视频功能"; 31 self.view.backgroundColor = [UIColor whiteColor]; 32 UIBarButtonItem *barBtnVideo = [[UIBarButtonItem alloc] 33 initWithBarButtonSystemItem:UIBarButtonSystemItemCamera 34 target:self 35 action:@selector(showActionSheet:)]; 36 self.toolbarItems = @[barBtnVideo]; 37 } 38 39 - (void)showActionSheet:(UIBarButtonItem *)sender { 40 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"录制视频" 41 delegate:self 42 cancelButtonTitle:@"取消" 43 destructiveButtonTitle:nil 44 otherButtonTitles:@"PhotoLibrary", @"Video", nil]; 45 [actionSheet showFromToolbar:self.navigationController.toolbar]; 46 } 47 48 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 49 if (error) { 50 NSLog(@"%@", [error localizedDescription]); 51 } 52 } 53 54 #pragma mark - UIActionSheetDelegate 55 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 56 if (buttonIndex != actionSheet.cancelButtonIndex) { 57 UIImagePickerControllerSourceType sourceType = buttonIndex; 58 if ([UIImagePickerController isSourceTypeAvailable:sourceType]) { 59 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 60 imagePickerController.delegate = self; 61 imagePickerController.sourceType = sourceType; 62 imagePickerController.videoQuality = UIImagePickerControllerQualityType640x480; //设置视频质量;默认值为UIImagePickerControllerQualityTypeMedium 63 imagePickerController.videoMaximumDuration = 30; //录制视频的时间,单位为秒;默认值为10分钟=600秒,这里设置为30秒 64 65 NSArray *arrMediaType = [UIImagePickerController availableMediaTypesForSourceType:sourceType]; 66 if ([arrMediaType containsObject:(NSString *)kUTTypeMovie]) { //为了使用kUTTypeMovie,这里需要导入<MobileCoreServices/MobileCoreServices.h> 67 imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie]; 68 } else { 69 NSLog(@"%@ is not available.", kUTTypeMovie); 70 } 71 72 [self presentViewController:imagePickerController 73 animated:YES 74 completion:nil]; 75 } 76 } 77 78 /* 79 UIImagePickerControllerQualityType(视频质量类型枚举):经过真机测试,录制30秒的视频,清晰度和大小由高到低为: 80 UIImagePickerControllerQualityTypeIFrame1280x720 -- 135.6 MB 81 UIImagePickerControllerQualityTypeIFrame960x540 -- 100.1 MB 82 UIImagePickerControllerQualityTypeHigh -- 61.8 MB 83 UIImagePickerControllerQualityType640x480 -- 12.7 MB(清晰度和大小来说,更推荐这个) 84 UIImagePickerControllerQualityTypeMedium -- 2.8 MB(默认值) 85 UIImagePickerControllerQualityTypeLow -- 721.4 KB(无法直视的模糊啊) 86 87 88 typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) { 89 UIImagePickerControllerQualityTypeHigh = 0, // highest quality 90 UIImagePickerControllerQualityTypeMedium = 1, // medium quality, suitable for transmission via Wi-Fi 91 UIImagePickerControllerQualityTypeLow = 2, // lowest quality, suitable for tranmission via cellular network 92 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 93 UIImagePickerControllerQualityType640x480 = 3, // VGA quality 94 #endif 95 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0 96 UIImagePickerControllerQualityTypeIFrame1280x720 = 4, 97 UIImagePickerControllerQualityTypeIFrame960x540 = 5 98 #endif 99 };100 */101 }102 103 #pragma mark - UIImagePickerControllerDelegate104 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {105 //判断是否是视频106 NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];107 if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {108 NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];109 NSString *mediaPath = [mediaURL path];110 //判断视频路径是否支持被保存到图片库中111 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(mediaPath)) {112 //将视频保存到相册113 UISaveVideoAtPathToSavedPhotosAlbum(mediaPath,114 self,115 @selector(video:didFinishSavingWithError:contextInfo:),116 NULL);117 } else {118 NSLog(@"视频路径不支持被保存到图片库中");119 }120 }121 122 123 //把图片保存到相册124 // UIImageWriteToSavedPhotosAlbum(imgChoice,125 // self,126 // @selector(image:didFinishSavingWithError:contextInfo:),127 // NULL);128 129 [self imagePickerControllerDidCancel:picker];130 }131 132 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {133 [self dismissViewControllerAnimated:YES completion:nil];134 }135 136 @end
AppDelegate.h
1 #import <UIKit/UIKit.h>2 3 @interface AppDelegate : UIResponder <UIapplicationDelegate>4 @property (strong, nonatomic) UIWindow *window;5 @property (strong, nonatomic) UINavigationController *navigationController;6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];11 ViewController *viewController = [[ViewController alloc] init];12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];13 _window.rootViewController = _navigationController;14 //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无15 [_window makeKeyAndVisible];16 return YES;17 }18 19 - (void)applicationWillResignActive:(UIApplication *)application {20 }21 22 - (void)applicationDidEnterBackground:(UIApplication *)application {23 }24 25 - (void)applicationWillEnterForeground:(UIApplication *)application {26 }27 28 - (void)applicationDidBecomeActive:(UIApplication *)application {29 }30 31 - (void)applicationWillTerminate:(UIApplication *)application {32 }33 34 @end
PrefixHeader.pch
1 #ifndef FirstBook187_PrefixHeader_pch2 #define FirstBook187_PrefixHeader_pch3 4 // 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>7 8 #endif
新闻热点
疑难解答