how to save video file into document directory

后端 未结 9 741
庸人自扰
庸人自扰 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:19
    #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];
    }
    
    0 讨论(0)
  • 2020-12-12 22:22

    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];
    }
    
    0 讨论(0)
  • 2020-12-12 22:27

    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)
    
    }
    
    0 讨论(0)
提交回复
热议问题