I am capturing video using following code:
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSo
Bit of R&D, this worked for me
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
// Save video to app document directory
NSString *filePath = [url path];
NSString *pathExtension = [filePath pathExtension] ;
if ([pathExtension length] > 0)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", [filePath lastPathComponent]]];
// Method last path component is used here, so that each video saved will get different name.
NSError *error = nil ;
BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
if (!res)
{
NSLog(@"%@", [error localizedDescription]) ;
}
else
{
NSLog(@"File saved at : %@",localFilePath);
}
}
}
//Also when you have to check for same video already exist in app document directory and you don't want create multiple copies of it then made some changes as below
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSURL *videoAsset = [info objectForKey:UIImagePickerControllerReferenceURL];
__weak typeof(self) weakSelf = self;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:videoAsset resultBlock:^(ALAsset *asset)
{
weakSelf.selectedFileName = [[asset defaultRepresentation] filename];
NSLog(@"Video Filename %@",weakSelf.selectedFileName);
// Save video to doc directory
NSString *filePath = [url path];
NSString *pathExtension = [filePath pathExtension] ;
if ([pathExtension length] > 0)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", weakSelf.selectedFileName]];
//check if same video is having its copy in app directory.
//so that multiple entries of same file should not happen.
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
if (!fileExists)
{
NSError *error = nil ;
BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
if (!res)
{
NSLog(@"%@", [error localizedDescription]) ;
}
else
{
NSLog(@"File saved at : %@",localFilePath);
weakSelf.filePathURL = [NSURL URLWithString:localFilePath];
}
}
else
{
NSLog(@"File exist at : %@",localFilePath);
weakSelf.filePathURL = [NSURL URLWithString:localFilePath];
}
}
}
}
//Where weakSelf.selectedFileName & weakSelf.filePathURL are NSString and NSURL type properties of my class respectively.