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(無法直視的模糊?。?/p>
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(無法直視的模糊?。?/span> 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
新聞熱點
疑難解答