how to save video file into document directory

后端 未结 9 744
庸人自扰
庸人自扰 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];
    }
    

提交回复
热议问题