how to save video file into document directory

后端 未结 9 782
庸人自扰
庸人自扰 2020-12-12 21:45

I am capturing video using following code:

UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType =  UIImagePickerControllerSo         


        
9条回答
  •  无人及你
    2020-12-12 22:16

    We can use NSFileManager's moveItemAtPath:toPath:error: method to move the video file to our app's document directory. It's more efficient.

    Also we should get the extension of the video file and use it as extension of our local video file name.

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [self dismissViewControllerAnimated:YES completion:nil] ;
    
        if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) {
            // video
            NSURL *url = [info[UIImagePickerControllerMediaType] ;
            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:@"temp.%@", pathExtension]] ;
                NSError *error = nil ;
                BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
                if (!res) {
                    NSLog(@"%@", [error localizedDescription]) ;
                }
            }
        }
    }
    

    It works for me.

提交回复
热议问题