I am capturing video using following code:
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSo
#pragma mark -
#pragma mark File Names and Paths
// Creates the path if it does not exist.
- (void)ensurePathAt:(NSString *)path {
NSFileManager *fm = [NSFileManager defaultManager];
if ( [fm fileExistsAtPath:path] == false ) {
[fm createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
}
- (NSString *)documentPath {
if ( ! documentPath_ ) {
NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentPath_ = [searchPaths objectAtIndex: 0];
documentPath_=[documentPath_ stringByAppendingPathComponent:@"VideoAlbum"];
[documentPath_ retain];
}
return documentPath_;
}
- (NSString *)audioPath {
if ( ! AudioPath_ ) {
AudioPath_ = [[self documentPath] stringByAppendingPathComponent:@"Demo"];
NSLog(@"%@",AudioPath_);
[AudioPath_ retain];
[self ensurePathAt:AudioPath_];
}
return AudioPath_;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
tempPath = [[self audioPath] stringByAppendingFormat:@"/%@.mp4",[NSDate date]];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
NSLog(@"%hhd",success);
}
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
Saving the video to the documents directory is as follows:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
[picker dismissModalViewControllerAnimated:YES];
}
Here is the swift code if anyone need it in future:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
let videoData = NSData(contentsOfURL: videoURL)
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent("/vid1.mp4")
videoData?.writeToFile(dataPath, atomically: false)
self.dismissViewControllerAnimated(true, completion: nil)
}