how to save video file into document directory

后端 未结 9 787
庸人自扰
庸人自扰 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:12

    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.

提交回复
热议问题