如何使用UIImagePickerController将视频上传到服务器

时间:2017-12-01 11:00:00

标签: ios objective-c uiimagepickercontroller

现在使用以下代码上传数据。但我需要上传mp4格式的二进制数据。现在我只获得允许移动格式的文件路径,但我需要为该路径转换mp4。

UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];

 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

[self presentViewController:imagePicker animated:YES completion:nil];



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{

    NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

}

1 个答案:

答案 0 :(得分:0)

您可以使用NSUrlSession以字节格式上传文件。将文件转换为NSData并将其附加到您的请求中。在imagePicker委托中,您可以这样做:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    NSData *videoData = [NSData dataWithContentsOfFile:videoPath];

    NSURLRequest *uploadRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"Your server url"]];
    [uploadRequest setHTTPMethod:@"POST"];

    NSURLSessionUploadTask *uploadTask = [[NSURLSession sharedSession] uploadTaskWithRequest: uploadRequest fromData: videoData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        //Handle your response here
    });

    [uploadTask resume];
}