You will need a few steps using the UIImagePickerController
:
Frameworks
Import the necessary frameworks in Linked Frameworks and Libraries:
- Assetslibrary.framework
- Mobilecoreservices.framework
And in your header file:
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>
Delegates
It will take 2 delegates, both for the Controller which will open for video capture. Also in your header file, include the two:
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Setting properties and opening the camera
Here you define some camera properties and how this Controller
will be opened, being a "modal" and after checking the existence of the camera on the device:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
[cameraPicker setModalPresentationStyle:UIModalPresentationCurrentContext];
[cameraPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[cameraPicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil]];
[cameraPicker setShowsCameraControls:YES];
[cameraPicker setCameraDevice:UIImagePickerControllerCameraDeviceFront];
[cameraPicker setDelegate:self];
[self presentViewController:cameraPicker animated:YES completion:nil];
}
These properties you can define yourself how you use them. Based on your question, already included to open the front camera with enabled controls.
Saving the video in the library
And finally, in the method of delegate didFinishPickingMediaWithInfo
, you will receive all the information and save the video in your library:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.movie"]) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);
if (compatible) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error) {
[picker dismissViewControllerAnimated:YES completion:nil];
}];
}
}
}
}
Note that here I have not included the else
, but you can do there, indicating if there was no video capture, there is no compatibility and it is not possible to save in the device library, either because it was not authorized or any other reason.
And this other delegate, only to close the camera in case the action is canceled:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
That’s it. I made a very simple project here, if there is a need I can make it available for download and you test from there.